0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-06 22:10:10 -05:00

Fix injected endpoint prerender detection ()

This commit is contained in:
Bjorn Lu 2024-09-25 15:14:04 +01:00 committed by GitHub
parent da4bc2c8bc
commit 1720c5b1d2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 73 additions and 31 deletions
.changeset
packages/astro

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes injected endpoint `prerender` option detection

View file

@ -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');
}

View file

@ -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 = {

View file

@ -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,
});
},
},
},
],
});

View file

@ -0,0 +1,9 @@
export const prerender = true;
export function getStaticPaths() {
return [{ params: { slug: 'test' } }];
}
export function GET() {
return new Response('OK — test');
}

View file

@ -1,17 +0,0 @@
---
import { manifest } from 'astro:ssr-manifest';
---
<html>
<head>
<title>Testing</title>
<style>
body {
background: green;
}
</style>
</head>
<body>
<h1>Testing</h1>
<div id="assets" set:html={JSON.stringify([...manifest.assets])}></div>
</body>
</html>

View file

@ -0,0 +1,5 @@
import { manifest } from 'astro:ssr-manifest';
export function GET() {
return Response.json(manifest);
}

View file

@ -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', () => {
@ -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();
assert.equal(app.manifest.compressHTML, true);
// NOTE: `app.manifest` is actually a private property
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);
});
});