Skip to content

Commit

Permalink
fix(vercel): include 404.html as fallback in the static adapter (#9591)
Browse files Browse the repository at this point in the history
* include 404.html as fallback

* add changeset

* include only when 404.astro exists

* add test

* route.component -> route.pathname
  • Loading branch information
lilnasy authored Jan 5, 2024
1 parent 64a8470 commit 22a5405
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-turtles-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/vercel": patch
---

Fixes an issue where 404.astro was not used in static mode.
5 changes: 5 additions & 0 deletions packages/integrations/vercel/src/static/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ export default function vercelStatic({
continue: true,
},
{ handle: 'filesystem' },
...routes.find(route => route.pathname === "/404") ? [{
src: `/.*`,
dest: `/404.html`,
status: 404,
}] : [],
],
...(imageService || imagesConfig
? {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from 'astro/config';
import vercel from '@astrojs/vercel/static';

export default defineConfig({
adapter: vercel()
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/astro-vercel-static",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>404</title>
</head>
<body>
<h1>404</h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>One</title>
</head>
<body>
<h1>One</h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Two</title>
</head>
<body>
<h1>Two</h1>
</body>
</html>
26 changes: 26 additions & 0 deletions packages/integrations/vercel/test/static.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';

describe('maxDuration', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/static/',
});
await fixture.build();
});

it('falls back to 404.html', async () => {
const deploymentConfig = JSON.parse(
await fixture.readFile('../.vercel/output/config.json')
);
// change the index if necesseary
expect(deploymentConfig.routes[2]).to.deep.include({
src: '/.*',
dest: '/404.html',
status: 404
});
});
});
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 22a5405

Please sign in to comment.