mirror of
https://github.com/withastro/astro.git
synced 2024-12-23 21:53:55 -05:00
Allow dynamic segments in injected routes (#5331)
* Allow dynamic segments in injected routes * Little better * Fallback for resolveId too
This commit is contained in:
parent
16abbad0b6
commit
688f8e4bc1
5 changed files with 68 additions and 16 deletions
5
.changeset/good-ghosts-attack.md
Normal file
5
.changeset/good-ghosts-attack.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Allow dynamic segments in injected routes
|
|
@ -352,27 +352,20 @@ export function createRouteManifest(
|
|||
)
|
||||
.reverse() // prepend to the routes array from lowest to highest priority
|
||||
.forEach(({ pattern: name, entryPoint }) => {
|
||||
const resolved = require.resolve(entryPoint, { paths: [cwd || fileURLToPath(config.root)] });
|
||||
let resolved: string;
|
||||
try {
|
||||
resolved = require.resolve(entryPoint, { paths: [cwd || fileURLToPath(config.root)] });
|
||||
} catch(e) {
|
||||
resolved = fileURLToPath(new URL(entryPoint, config.root));
|
||||
}
|
||||
const component = slash(path.relative(cwd || fileURLToPath(config.root), resolved));
|
||||
|
||||
const isDynamic = (str: string) => str?.[0] === '[';
|
||||
const normalize = (str: string) => str?.substring(1, str?.length - 1);
|
||||
|
||||
const segments = removeLeadingForwardSlash(name)
|
||||
.split(path.sep)
|
||||
.split(path.posix.sep)
|
||||
.filter(Boolean)
|
||||
.map((s: string) => {
|
||||
validateSegment(s);
|
||||
|
||||
const dynamic = isDynamic(s);
|
||||
const content = dynamic ? normalize(s) : s;
|
||||
return [
|
||||
{
|
||||
content,
|
||||
dynamic,
|
||||
spread: isSpread(s),
|
||||
},
|
||||
];
|
||||
return getParts(s, component);
|
||||
});
|
||||
|
||||
const type = resolved.endsWith('.astro') ? 'page' : 'endpoint';
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import nodeFs from 'fs';
|
||||
import npath from 'path';
|
||||
import slashify from 'slash';
|
||||
import type * as vite from 'vite';
|
||||
import type { AstroSettings } from '../@types/astro';
|
||||
|
||||
|
@ -41,9 +42,15 @@ export default function loadFallbackPlugin({
|
|||
{
|
||||
name: 'astro:load-fallback',
|
||||
enforce: 'post',
|
||||
resolveId(id, parent) {
|
||||
async resolveId(id, parent) {
|
||||
if (id.startsWith('.') && parent && fs.existsSync(parent)) {
|
||||
return npath.posix.join(npath.posix.dirname(parent), id);
|
||||
} else {
|
||||
let resolved = await this.resolve(id, parent, { skipSelf: true });
|
||||
if(resolved) {
|
||||
return resolved.id;
|
||||
}
|
||||
return slashify(id);
|
||||
}
|
||||
},
|
||||
async load(id) {
|
||||
|
|
|
@ -106,4 +106,49 @@ describe('dev container', () => {
|
|||
expect($('body.two')).to.have.a.lengthOf(1);
|
||||
});
|
||||
});
|
||||
|
||||
it('Allows dynamic segments in injected routes', async () => {
|
||||
const fs = createFs({
|
||||
'/src/components/test.astro': `<h1>{Astro.params.slug}</h1>`,
|
||||
'/src/pages/test-[slug].astro': `<h1>{Astro.params.slug}</h1>`,
|
||||
},
|
||||
root
|
||||
);
|
||||
|
||||
await runInContainer({
|
||||
fs,
|
||||
root,
|
||||
userConfig: {
|
||||
output: 'server',
|
||||
integrations: [{
|
||||
name: '@astrojs/test-integration',
|
||||
hooks: {
|
||||
'astro:config:setup': ({ injectRoute }) => {
|
||||
injectRoute({
|
||||
pattern: '/another-[slug]',
|
||||
entryPoint: './src/components/test.astro',
|
||||
});
|
||||
},
|
||||
},
|
||||
}]
|
||||
}
|
||||
}, async (container) => {
|
||||
let r = createRequestAndResponse({
|
||||
method: 'GET',
|
||||
url: '/test-one',
|
||||
});
|
||||
container.handle(r.req, r.res);
|
||||
await r.done;
|
||||
expect(r.res.statusCode).to.equal(200);
|
||||
|
||||
// Try with the injected route
|
||||
r = createRequestAndResponse({
|
||||
method: 'GET',
|
||||
url: '/another-two',
|
||||
});
|
||||
container.handle(r.req, r.res);
|
||||
await r.done;
|
||||
expect(r.res.statusCode).to.equal(200);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -15,6 +15,8 @@ class MyVolume extends Volume {
|
|||
#forcePath(p) {
|
||||
if (p instanceof URL) {
|
||||
p = unixify(fileURLToPath(p));
|
||||
} else {
|
||||
p = unixify(p);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue