-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show injected custom 404 route in dev (#6940)
* Add unit test for injected 404 routes * Add fixture test for injected 404 routes * Use any route pattern to find custom 404 * Fix frozen lockfile error * Use `route` instead of `pattern` to match custom 404 Dynamic catch-all routes can match against `/404/` but will then error because they’re not actually designed to handle a param of `'404'`. Testing `route` instead excludes dynamic routes (because they’ll contain dynamic segments inside square brackets). Not sure if someone might _want_ to render the 404 via a dynamic route, but we already don’t support that, so this doesn’t change anything. * Fix injected 404 fixture * Add tests for `src/pages/404.html` * Add changeset * Fix lockfile * And again
- Loading branch information
Showing
14 changed files
with
250 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Support custom 404s added via `injectRoute` or as `src/pages/404.html` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { expect } from 'chai'; | ||
import * as cheerio from 'cheerio'; | ||
import { loadFixture } from './test-utils.js'; | ||
|
||
describe('Custom 404.html', () => { | ||
let fixture; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/custom-404-html/', | ||
site: 'http://example.com', | ||
}); | ||
}); | ||
|
||
describe('dev', () => { | ||
let devServer; | ||
let $; | ||
|
||
before(async () => { | ||
devServer = await fixture.startDevServer(); | ||
}); | ||
|
||
after(async () => { | ||
await devServer.stop(); | ||
}); | ||
|
||
it('renders /', async () => { | ||
const html = await fixture.fetch('/').then((res) => res.text()); | ||
$ = cheerio.load(html); | ||
|
||
expect($('h1').text()).to.equal('Home'); | ||
}); | ||
|
||
it('renders 404 for /a', async () => { | ||
const html = await fixture.fetch('/a').then((res) => res.text()); | ||
$ = cheerio.load(html); | ||
|
||
expect($('h1').text()).to.equal('Page not found'); | ||
expect($('p').text()).to.equal('This 404 is a static HTML file.'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { expect } from 'chai'; | ||
import * as cheerio from 'cheerio'; | ||
import { loadFixture } from './test-utils.js'; | ||
|
||
describe('Custom 404 with injectRoute', () => { | ||
let fixture; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/custom-404-injected/', | ||
site: 'http://example.com', | ||
}); | ||
}); | ||
|
||
describe('dev', () => { | ||
let devServer; | ||
let $; | ||
|
||
before(async () => { | ||
devServer = await fixture.startDevServer(); | ||
}); | ||
|
||
after(async () => { | ||
await devServer.stop(); | ||
}); | ||
|
||
it('renders /', async () => { | ||
const html = await fixture.fetch('/').then((res) => res.text()); | ||
$ = cheerio.load(html); | ||
|
||
expect($('h1').text()).to.equal('Home'); | ||
}); | ||
|
||
it('renders 404 for /a', async () => { | ||
const html = await fixture.fetch('/a').then((res) => res.text()); | ||
$ = cheerio.load(html); | ||
|
||
expect($('h1').text()).to.equal('Page not found'); | ||
expect($('p').text()).to.equal('/a'); | ||
}); | ||
}); | ||
}); |
4 changes: 4 additions & 0 deletions
4
packages/astro/test/fixtures/custom-404-html/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { defineConfig } from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@test/custom-404-html", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"astro": "workspace:*" | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/astro/test/fixtures/custom-404-html/src/pages/404.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<html lang="en"> | ||
<head> | ||
<title>Not Found - Custom 404</title> | ||
</head> | ||
<body> | ||
<h1>Page not found</h1> | ||
<p>This 404 is a static HTML file.</p> | ||
</body> | ||
</html> |
11 changes: 11 additions & 0 deletions
11
packages/astro/test/fixtures/custom-404-html/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
--- | ||
|
||
<html lang="en"> | ||
<head> | ||
<title>Custom 404</title> | ||
</head> | ||
<body> | ||
<h1>Home</h1> | ||
</body> | ||
</html> |
18 changes: 18 additions & 0 deletions
18
packages/astro/test/fixtures/custom-404-injected/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { defineConfig } from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
integrations: [ | ||
{ | ||
name: '404-integration', | ||
hooks: { | ||
'astro:config:setup': ({ injectRoute }) => { | ||
injectRoute({ | ||
pattern: '404', | ||
entryPoint: 'src/404.astro', | ||
}); | ||
}, | ||
}, | ||
}, | ||
], | ||
}); |
8 changes: 8 additions & 0 deletions
8
packages/astro/test/fixtures/custom-404-injected/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@test/custom-404-injected", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"astro": "workspace:*" | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/astro/test/fixtures/custom-404-injected/src/404.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
const canonicalURL = new URL(Astro.url.pathname, Astro.site); | ||
--- | ||
|
||
<html lang="en"> | ||
<head> | ||
<title>Not Found - Custom 404</title> | ||
</head> | ||
<body> | ||
<h1>Page not found</h1> | ||
<p>{canonicalURL.pathname}</p> | ||
</body> | ||
</html> |
11 changes: 11 additions & 0 deletions
11
packages/astro/test/fixtures/custom-404-injected/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
--- | ||
|
||
<html lang="en"> | ||
<head> | ||
<title>Custom 404</title> | ||
</head> | ||
<body> | ||
<h1>Home</h1> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.