Skip to content

Commit

Permalink
Fix injectRoute for SSR (#7128)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
johannesspohr and ematipico authored May 19, 2023
1 parent ed4aff0 commit 72f686a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-garlics-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix routes created by `injectRoute` for SSR
28 changes: 10 additions & 18 deletions packages/astro/src/core/build/plugins/plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
});
}

Expand Down
18 changes: 18 additions & 0 deletions packages/astro/test/ssr-dynamic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 72f686a

Please sign in to comment.