Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(prerendering): never prerender custom 500 routes #8031

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wet-snakes-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

fixed an edge case where attempting to prerender a 500 route led to missing styles and scripts
15 changes: 14 additions & 1 deletion packages/astro/src/vite-plugin-scanner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { AstroSettings } from '../@types/astro.js';
import { type LogOptions } from '../core/logger/core.js';

import { bold } from 'kleur/colors';
import { extname } from 'node:path';
import { basename, extname } from 'node:path';
import { normalizePath } from 'vite';
import { warn } from '../core/logger/core.js';
import { isEndpoint, isPage, rootRelativePath } from '../core/util.js';
Expand All @@ -15,6 +15,14 @@ export interface AstroPluginScannerOptions {
logging: LogOptions;
}

function isPrerenderEligible(id: string) {
const base = basename(id)
// astro core doesn't use a prerendered 404.astro
// but netlify adapter achieves the effect by using redirects
// so it would still be a regression to consider it ineligible
return /* !base.startsWith("404") && */ !base.startsWith("500")
}

const KNOWN_FILE_EXTENSIONS = ['.astro', '.js', '.ts'];

export default function astroScannerPlugin({
Expand Down Expand Up @@ -46,6 +54,11 @@ export default function astroScannerPlugin({
if (typeof pageOptions.prerender === 'undefined') {
pageOptions.prerender = defaultPrerender;
}

if (!isPrerenderEligible(id)) {
pageOptions.prerender = false;
}

// `getStaticPaths` warning is just a string check, should be good enough for most cases
if (
!pageOptions.prerender &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: ivory;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

---
import "../external-stylesheet.css"
export const prerender = true
---
<h1>{new Date(Date.now()).toISOString()}</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
return new Response(null, { status: 500 })
---
<p>This html will not be served</p>
21 changes: 19 additions & 2 deletions packages/astro/test/ssr-prerender.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,25 @@ describe('SSR: prerender', () => {
const app = await fixture.loadTestAdapterApp();
/** @type {Set<string>} */
const assets = app.manifest.assets;
expect(assets.size).to.equal(1);
expect(Array.from(assets)[0].endsWith('static/index.html')).to.be.true;
expect(assets).to.contain('/static/index.html');
});

// remove when support for prerendered 404 and 500 routes is added
it('ignores prerender = true for 500 routes', async () => {
const app = await fixture.loadTestAdapterApp();
const fiveHundredRoute = app.manifest.routes.find(route => route.routeData.route === '/500');
expect(fiveHundredRoute.routeData.prerender).to.equal(false);
});

it('includes expected styles in a redirected 500 route that has `prerender = true`', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/fivehundred');
const response = await app.render(request);
const html = await response.text();
const $ = cheerio.load(html);

// length will be 0 if the stylesheet does not get included
expect($('link[rel=stylesheet]')).to.have.a.lengthOf(1);
});
});

Expand Down