Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ovyerus/astro-worker-links
Automatically create shortened urls for your Astro project via worker-links!
https://github.com/ovyerus/astro-worker-links
astro cloudflare cloudflare-workers link-shortener worker-links
Last synced: 4 months ago
JSON representation
Automatically create shortened urls for your Astro project via worker-links!
- Host: GitHub
- URL: https://github.com/ovyerus/astro-worker-links
- Owner: Ovyerus
- License: mit
- Created: 2023-01-22T13:20:44.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-13T07:34:24.000Z (about 1 year ago)
- Last Synced: 2024-10-11T04:41:55.190Z (4 months ago)
- Topics: astro, cloudflare, cloudflare-workers, link-shortener, worker-links
- Language: TypeScript
- Homepage:
- Size: 2.74 MB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# astro-worker-links
Automatically create [worker-links](https://github.com/erisa/worker-links)
redirects for your [Astro](https://astro.build/) pages at build time!Better docs will come soon, it's late rn and I'm just getting this out the door.
## Example usage
```js
// astro.config.mjs
import { defineConfig } from "astro/config";
import workerLinks from "astro-worker-links";// https://astro.build/config
export default defineConfig({
integrations: [
// ...
workerLinks({
domain: "https://my-worker-links.example.com",
secret: process.env.WORKER_LINKS_SECRET,
// Pages is an array of all the built pages from your project as URLs
getPageMapping(pages) {
// Filter to only generate links for pages matching `/blog/[id]` anywhere in them (but not the index page)
return pages
.filter(
(url) => url.pathname !== "/blog/" && url.pathname.includes("/blog")
)
.map((url) => {
return {
page: url.href,
// Strip the `/blog` intermediate part so that short links are just `example.com/blog-post-name` instead of `example.com/blog/blog-post-name`
shortlink: url.pathname.replace("/blog", ""),
};
});
},
}),
],
// ...
});
```you can also provide an async function for `getPageMapping` if you need to fetch
external data```js
export default defineConfig({
integrations: [
// ...
workerLinks({
// ...
async getPageMapping(pages) {
const results = await Promise.all(
pages.map(async (url) => {
const resource = await fetch(...);
return {
page: url.href,
shortlink: resource.id, // or whatever
};
})
);return results;
},
}),
],
});
```Or you can just return a static object from the function
```js
export default defineConfig({
integrations: [
// ...
workerLinks({
// ...
getPageMapping() {
return [
{
page: "https://really-long-site.example.com/blog/my-awesome-post-please-read-it",
shortlink: "/my-awesome-post",
},
{
page: "https://really-long-site.example.com/about-us",
shortlink: "/about",
},
];
},
}),
],
});
```## License
The source code in this repository is licensed under the
[MIT License](./LICENSE).