Skip to content
Merged
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/error-page-astro-cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes `Astro.cache` being `undefined` when a custom 404 or 500 page is rendered by the error handler, for example after a request to an API route with an HTTP method the route doesn't export
2 changes: 2 additions & 0 deletions packages/astro/src/core/errors/default-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { handleMiddleware } from '../middleware/astro-middleware.js';
import { handlePages } from '../pages/handler.js';
import { matchRoute } from '../routing/match.js';
import { getRouteTable } from '../routing/route-table.js';
import { provideCache } from '../cache/handler.js';
import { provideSession } from '../session/provider.js';
import { validateHost } from '../app/validate-headers.js';
import { getErrorRoutePath } from '../../i18n/error-routes.js';
Expand Down Expand Up @@ -113,6 +114,7 @@ export async function renderDefaultError(
errorState.initialProps = { error };
try {
await provideSession(errorState);
await provideCache(errorState);
const response = await handleMiddleware(errorState, handlePages);
// A middleware rewrite (`ctx.rewrite()` / `next(payload)`) issued while
// rendering the error page swaps the state's routeData away from the
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/core/errors/dev-handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { RenderErrorOptions } from '../app/base.js';
import type { SSRManifest } from '../app/types.js';
import { provideCache } from '../cache/handler.js';
import { getEnvironment } from '../environment/index.js';
import { FetchState } from '../fetch/fetch-state.js';
import type { RouteData } from '../../types/public/index.js';
Expand Down Expand Up @@ -66,6 +67,7 @@ export async function renderDevError(
errorState.componentInstance = preloadedComponent;
errorState.locals = resolvedRenderOptions.locals ?? ({} as App.Locals);
errorState.initialProps = { error };
await provideCache(errorState);
const response = await handleMiddleware(errorState, handlePages);

// A middleware rewrite issued while rendering the error page swaps the
Expand Down
15 changes: 15 additions & 0 deletions packages/astro/test/units/cache/app-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,21 @@ describe('context.cache disabled (no provider configured)', () => {
const body = await response.json();
assert.deepEqual(body, { ok: true });
});

it('Astro.cache is defined on the 404 page rendered for an unhandled API route method', async () => {
const notFoundPage = createComponent((result, props, slots) => {
const Astro = result.createAstro(props, slots);
Astro.cache.set({ maxAge: 120 });
return render`<html><head>${renderHead()}</head><body><h1>Not Found</h1></body></html>`;
});
const app = createTestApp([
createEndpoint({ POST: () => new Response('ok') }, { route: '/api/thing' }),
createPage(notFoundPage, { route: '/404' }),
]);
const response = await app.render(new Request('http://example.com/api/thing'));
assert.equal(response.status, 404);
assert.match(await response.text(), /<h1>Not Found<\/h1>/);
});
});

// #endregion
Loading