Skip to content

Commit

Permalink
Add support for strict custom routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcia Alves authored and Marcia Alves committed Jan 26, 2024
1 parent 561aef6 commit 39f61a4
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 274 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-masks-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inox-tools/custom-routing": minor
---

Add support for strict custom routes
12 changes: 11 additions & 1 deletion docs/src/content/docs/custom-routing/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@ export default defineConfig({
});
```

### License
## Operating modes

There are two integrations available in this package, `customRouting` and `strictCustomRouting`.
Both receive the same parameters and behave the same regarding your custom routes.

The `strictCustomRouting` ensures your project routing is strictly custom. When using it your project will
fail to build in case any route is generated from the file-based routing of `src/pages`.

If you want to use both custom routing and file-based routing in the same project, use `customRouting`.

## License

Custom Routing is available under the MIT license.

4 changes: 2 additions & 2 deletions examples/custom-routing/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { defineConfig } from 'astro/config';
import mdx from '@astrojs/mdx';
import sitemap from '@astrojs/sitemap';
import { customRouting } from '@inox-tools/custom-routing';
import { strictCustomRouting } from '@inox-tools/custom-routing';

// https://astro.build/config
export default defineConfig({
site: 'https://example.com',
integrations: [
mdx(),
sitemap(),
customRouting({
strictCustomRouting({
'': './src/routes/index.astro',
'about': './src/routes/about.astro',
'blog': './src/routes/blog/index.astro',
Expand Down
35 changes: 27 additions & 8 deletions packages/custom-routing/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type {AstroIntegration, APIRoute} from 'astro';
import type { AstroIntegration } from 'astro';
import { AstroError } from 'astro/errors';
import { fileURLToPath } from 'node:url';
import { resolve, sep as PATH_SEP } from 'node:path';

// TODO: Add support for inline API route once Astro core accepts virtual modules
// as injected routes.
Expand All @@ -9,25 +12,34 @@ type Options = {
routes: Record<string, CustomRoute>,
};

function customRoutingInner({strict, routes}, Options): AstroIntegration {
function customRoutingInner({ strict, routes }: Options): AstroIntegration {
return {
name: '@inox-tools/custom-routing',
hooks: {
'astro:config:setup': ({injectRoute}) => {
'astro:config:setup': ({ injectRoute }) => {
for (const [route, handle] of Object.entries(routes)) {
injectRoute({
entrypoint: handle,
pattern: route,
});
}
},
'astro:build:setup': ({pages}) => {
for (const [route, page] of pages.entries()) {
console.log({route, page});
...(strict && {
'astro:build:setup': ({ vite, pages }) => {
const pagesFolder = resolve(vite.root ?? '', './src/pages') + PATH_SEP;
for (const page of pages.values()) {
const componentPath = resolve(vite.root ?? '', page.component);
if (componentPath.startsWith(pagesFolder)) {
throw new AstroError(
'Custom routing used alongside pages route.',
'Either use disable strict mode of custom routing or remove any file-based routes.'
);
}
}
}
}
}),
}
}
};
}

export function customRouting(routes: Options['routes']): AstroIntegration {
Expand All @@ -36,3 +48,10 @@ export function customRouting(routes: Options['routes']): AstroIntegration {
routes: routes,
});
}

export function strictCustomRouting(routes: Options['routes']): AstroIntegration {
return customRoutingInner({
strict: true,
routes: routes,
});
}
3 changes: 3 additions & 0 deletions packages/custom-routing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@
},
"peerDependency": {
"astro": ">=3"
},
"devDependencies": {
"astro": "^4.2.1"
}
}
Loading

0 comments on commit 39f61a4

Please sign in to comment.