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)) {
if (!pageData.route.prerender) continue;
if (!pageData.route.pathname) continue;
for (const route of opts.manifest.routes) {
if (!route.prerender) continue;
if (!route.pathname) continue;
const outFolder = getOutFolder(
opts.settings.config,
pageData.route.pathname!,
pageData.route.type
);
const outFile = getOutFile(
opts.settings.config,
outFolder,
pageData.route.pathname!,
pageData.route.type
);
const outFolder = getOutFolder(opts.settings.config, route.pathname!, route.type);
const outFile = getOutFile(opts.settings.config, outFolder, route.pathname!, route.type);
const file = outFile.toString().replace(opts.settings.config.build.client.toString(), '');
routes.push({
file,
links: [],
scripts: [],
styles: [],
routeData: serializeRouteData(pageData.route, settings.config.trailingSlash),
routeData: serializeRouteData(route, settings.config.trailingSlash),
});
staticFiles.push(file);
}
for (const pageData of eachPageData(internals)) {
if (pageData.route.prerender) continue;
for (const route of opts.manifest.routes) {
const pageData = internals.pagesByComponent.get(route.component);
if (route.prerender || !pageData) continue;
const scripts: SerializedRouteInfo['scripts'] = [];
if (pageData.hoistedScript) {
const hoistedValue = pageData.hoistedScript.value;
@ -217,7 +209,7 @@ function buildManifest(
.map(({ stage, content }) => ({ stage, children: content })),
],
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({
root: './fixtures/ssr-dynamic/',
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(),
});
await fixture.build();
@ -55,6 +68,11 @@ describe('Dynamic pages in SSR', () => {
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 () => {
const favicon = await matchRoute('/favicon.ico');
expect(favicon).to.equal(undefined);