From 1720c5b1d2bfd106ad065833823aed622bee09bc Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Wed, 25 Sep 2024 15:14:04 +0100 Subject: [PATCH] Fix injected endpoint prerender detection (#12043) --- .changeset/lemon-flowers-tease.md | 5 +++ packages/astro/src/core/util.ts | 2 +- .../astro/src/vite-plugin-scanner/index.ts | 10 +++++- .../fixtures/ssr-manifest/astro.config.mjs | 22 ++++++++++++ .../fixtures/ssr-manifest/entrypoint-test.js | 9 +++++ .../ssr-manifest/src/pages/index.astro | 17 ---------- .../ssr-manifest/src/pages/manifest.json.js | 5 +++ packages/astro/test/ssr-manifest.test.js | 34 ++++++++++++------- 8 files changed, 73 insertions(+), 31 deletions(-) create mode 100644 .changeset/lemon-flowers-tease.md create mode 100644 packages/astro/test/fixtures/ssr-manifest/astro.config.mjs create mode 100644 packages/astro/test/fixtures/ssr-manifest/entrypoint-test.js delete mode 100644 packages/astro/test/fixtures/ssr-manifest/src/pages/index.astro create mode 100644 packages/astro/test/fixtures/ssr-manifest/src/pages/manifest.json.js diff --git a/.changeset/lemon-flowers-tease.md b/.changeset/lemon-flowers-tease.md new file mode 100644 index 000000000000..e4b2b3f5f13b --- /dev/null +++ b/.changeset/lemon-flowers-tease.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes injected endpoint `prerender` option detection diff --git a/packages/astro/src/core/util.ts b/packages/astro/src/core/util.ts index 654d1982994c..a6a890137cc9 100644 --- a/packages/astro/src/core/util.ts +++ b/packages/astro/src/core/util.ts @@ -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'); } diff --git a/packages/astro/src/vite-plugin-scanner/index.ts b/packages/astro/src/vite-plugin-scanner/index.ts index 05889c074afb..945f3d8ee64d 100644 --- a/packages/astro/src/vite-plugin-scanner/index.ts +++ b/packages/astro/src/vite-plugin-scanner/index.ts @@ -82,8 +82,16 @@ async function getPageOptions( settings: AstroSettings, logger: Logger, ): Promise { + 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 = { diff --git a/packages/astro/test/fixtures/ssr-manifest/astro.config.mjs b/packages/astro/test/fixtures/ssr-manifest/astro.config.mjs new file mode 100644 index 000000000000..6a605bd77b87 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-manifest/astro.config.mjs @@ -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, + }); + }, + }, + }, + ], +}); diff --git a/packages/astro/test/fixtures/ssr-manifest/entrypoint-test.js b/packages/astro/test/fixtures/ssr-manifest/entrypoint-test.js new file mode 100644 index 000000000000..457e89bf7df2 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-manifest/entrypoint-test.js @@ -0,0 +1,9 @@ +export const prerender = true; + +export function getStaticPaths() { + return [{ params: { slug: 'test' } }]; +} + +export function GET() { + return new Response('OK — test'); +} diff --git a/packages/astro/test/fixtures/ssr-manifest/src/pages/index.astro b/packages/astro/test/fixtures/ssr-manifest/src/pages/index.astro deleted file mode 100644 index f189e711c19a..000000000000 --- a/packages/astro/test/fixtures/ssr-manifest/src/pages/index.astro +++ /dev/null @@ -1,17 +0,0 @@ ---- -import { manifest } from 'astro:ssr-manifest'; ---- - - - Testing - - - -

Testing

-
- - diff --git a/packages/astro/test/fixtures/ssr-manifest/src/pages/manifest.json.js b/packages/astro/test/fixtures/ssr-manifest/src/pages/manifest.json.js new file mode 100644 index 000000000000..41ae39f405da --- /dev/null +++ b/packages/astro/test/fixtures/ssr-manifest/src/pages/manifest.json.js @@ -0,0 +1,5 @@ +import { manifest } from 'astro:ssr-manifest'; + +export function GET() { + return Response.json(manifest); +} diff --git a/packages/astro/test/ssr-manifest.test.js b/packages/astro/test/ssr-manifest.test.js index eaab834689a1..254ea304cd12 100644 --- a/packages/astro/test/ssr-manifest.test.js +++ b/packages/astro/test/ssr-manifest.test.js @@ -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(); + // 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); }); });