Skip to content

Commit

Permalink
Fix injected endpoint prerender detection (#12043)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Sep 25, 2024
1 parent da4bc2c commit 1720c5b
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-flowers-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes injected endpoint `prerender` option detection
2 changes: 1 addition & 1 deletion packages/astro/src/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export function isPage(file: URL, settings: AstroSettings): boolean {
}

export function isEndpoint(file: URL, settings: AstroSettings): boolean {
if (!isInPagesDir(file, settings.config)) return false;
if (!isInPagesDir(file, settings.config) && !isInjectedRoute(file, settings)) return false;
if (!isPublicRoute(file, settings.config)) return false;
return !endsWithPageExt(file, settings) && !file.toString().includes('?astro');
}
Expand Down
10 changes: 9 additions & 1 deletion packages/astro/src/vite-plugin-scanner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,16 @@ async function getPageOptions(
settings: AstroSettings,
logger: Logger,
): Promise<PageOptions> {
const fileUrlStr = fileURL.toString();
const injectedRoute = settings.resolvedInjectedRoutes.find(
(route) => route.resolvedEntryPoint && fileUrlStr === route.resolvedEntryPoint.toString(),
);

// Run initial scan
const pageOptions = await scan(code, id, settings);
const pageOptions =
injectedRoute?.prerender != null
? { prerender: injectedRoute.prerender }
: await scan(code, id, settings);

// Run integration hooks to alter page options
const route: RouteOptions = {
Expand Down
22 changes: 22 additions & 0 deletions packages/astro/test/fixtures/ssr-manifest/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { defineConfig } from 'astro/config';
import testAdapter from '../../test-adapter.js';
import { fileURLToPath } from 'url';

export default defineConfig({
output: 'server',
adapter: testAdapter(),
integrations: [
{
name: 'test',
hooks: {
'astro:config:setup'({ injectRoute }) {
injectRoute({
entrypoint: fileURLToPath(new URL('./entrypoint-test.js', import.meta.url)),
pattern: '[...slug]',
prerender: true,
});
},
},
},
],
});
9 changes: 9 additions & 0 deletions packages/astro/test/fixtures/ssr-manifest/entrypoint-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const prerender = true;

export function getStaticPaths() {
return [{ params: { slug: 'test' } }];
}

export function GET() {
return new Response('OK — test');
}
17 changes: 0 additions & 17 deletions packages/astro/test/fixtures/ssr-manifest/src/pages/index.astro

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { manifest } from 'astro:ssr-manifest';

export function GET() {
return Response.json(manifest);
}
34 changes: 22 additions & 12 deletions packages/astro/test/ssr-manifest.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import * as cheerio from 'cheerio';
import testAdapter from './test-adapter.js';
import { loadFixture } from './test-utils.js';

describe('astro:ssr-manifest', () => {
Expand All @@ -11,27 +9,39 @@ describe('astro:ssr-manifest', () => {
before(async () => {
fixture = await loadFixture({
root: './fixtures/ssr-manifest/',
output: 'server',
adapter: testAdapter(),
// test suite was authored when inlineStylesheets defaulted to never
build: { inlineStylesheets: 'never' },
});
await fixture.build();
});

it('works', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/');
const request = new Request('http://example.com/manifest.json');
const response = await app.render(request);
const html = await response.text();

const $ = cheerio.load(html);
assert.match($('#assets').text(), /\["\/_astro\/index.([\w-]{8})\.css"\]/);
const manifest = await response.json();
assert.equal(typeof manifest, 'object');
assert.equal(manifest.adapterName, 'my-ssr-adapter');
});

it('includes compressHTML', async () => {
const app = await fixture.loadTestAdapterApp();
// NOTE: `app.manifest` is actually a private property
assert.equal(app.manifest.compressHTML, true);
assert.equal(app.manifest.compressHTML, true);
});

it('includes correct routes', async () => {
const app = await fixture.loadTestAdapterApp();
// NOTE: `app.manifest` is actually a private property

const manifestJsonEndpoint = app.manifest.routes.find(
(route) => route.routeData.route === '/manifest.json',
);
assert.ok(manifestJsonEndpoint);
assert.equal(manifestJsonEndpoint.routeData.prerender, false);

// There should be no route for prerendered injected routes
const injectedEndpoint = app.manifest.routes.find(
(route) => route.routeData.route === '/[...slug]',
);
assert.equal(injectedEndpoint, undefined);
});
});

0 comments on commit 1720c5b

Please sign in to comment.