Fix injectRoute for SSR (#7128)

* Use manifest routes for SSR app manifest instead of page components to enable injected routes with SSR

* Small refactoring

Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>

---------

Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
This commit is contained in:
Johannes Spohr 2023-05-19 16:52:46 +02:00 committed by GitHub
parent ed4aff0cb9
commit 72f686a689
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 18 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix routes created by `injectRoute` for SSR

View file

@ -151,34 +151,26 @@ function buildManifest(
} }
}; };
for (const pageData of eachPageData(internals)) { for (const route of opts.manifest.routes) {
if (!pageData.route.prerender) continue; if (!route.prerender) continue;
if (!pageData.route.pathname) continue; if (!route.pathname) continue;
const outFolder = getOutFolder( const outFolder = getOutFolder(opts.settings.config, route.pathname!, route.type);
opts.settings.config, const outFile = getOutFile(opts.settings.config, outFolder, route.pathname!, route.type);
pageData.route.pathname!,
pageData.route.type
);
const outFile = getOutFile(
opts.settings.config,
outFolder,
pageData.route.pathname!,
pageData.route.type
);
const file = outFile.toString().replace(opts.settings.config.build.client.toString(), ''); const file = outFile.toString().replace(opts.settings.config.build.client.toString(), '');
routes.push({ routes.push({
file, file,
links: [], links: [],
scripts: [], scripts: [],
styles: [], styles: [],
routeData: serializeRouteData(pageData.route, settings.config.trailingSlash), routeData: serializeRouteData(route, settings.config.trailingSlash),
}); });
staticFiles.push(file); staticFiles.push(file);
} }
for (const pageData of eachPageData(internals)) { for (const route of opts.manifest.routes) {
if (pageData.route.prerender) continue; const pageData = internals.pagesByComponent.get(route.component);
if (route.prerender || !pageData) continue;
const scripts: SerializedRouteInfo['scripts'] = []; const scripts: SerializedRouteInfo['scripts'] = [];
if (pageData.hoistedScript) { if (pageData.hoistedScript) {
const hoistedValue = pageData.hoistedScript.value; const hoistedValue = pageData.hoistedScript.value;
@ -217,7 +209,7 @@ function buildManifest(
.map(({ stage, content }) => ({ stage, children: content })), .map(({ stage, content }) => ({ stage, children: content })),
], ],
styles, styles,
routeData: serializeRouteData(pageData.route, settings.config.trailingSlash), routeData: serializeRouteData(route, settings.config.trailingSlash),
}); });
} }

View file

@ -11,6 +11,19 @@ describe('Dynamic pages in SSR', () => {
fixture = await loadFixture({ fixture = await loadFixture({
root: './fixtures/ssr-dynamic/', root: './fixtures/ssr-dynamic/',
output: 'server', output: 'server',
integrations: [
{
name: 'inject-routes',
hooks: {
'astro:config:setup': ({ injectRoute }) => {
injectRoute({
pattern: '/path-alias/[id]',
entryPoint: './src/pages/api/products/[id].js',
});
},
},
},
],
adapter: testAdapter(), adapter: testAdapter(),
}); });
await fixture.build(); await fixture.build();
@ -55,6 +68,11 @@ describe('Dynamic pages in SSR', () => {
expect(json.id).to.equal('33'); expect(json.id).to.equal('33');
}); });
it('Injected route work', async () => {
const json = await fetchJSON('/path-alias/33');
expect(json.id).to.equal('33');
});
it('Public assets take priority', async () => { it('Public assets take priority', async () => {
const favicon = await matchRoute('/favicon.ico'); const favicon = await matchRoute('/favicon.ico');
expect(favicon).to.equal(undefined); expect(favicon).to.equal(undefined);