-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Show injected custom 404 route in dev #6940
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d4abb8b
Add unit test for injected 404 routes
delucis e81a8dd
Add fixture test for injected 404 routes
delucis ed61d83
Use any route pattern to find custom 404
delucis 9015e5d
Fix frozen lockfile error
delucis 7e73502
Use `route` instead of `pattern` to match custom 404
delucis 79d579a
Fix injected 404 fixture
delucis b88b478
Add tests for `src/pages/404.html`
delucis 1645d28
Add changeset
delucis dcd53a5
Fix lockfile
delucis 80ca780
And again
delucis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -158,6 +158,68 @@ describe('dev container', () => { | |
); | ||
}); | ||
|
||
it('Serves injected 404 route for any 404', async () => { | ||
const fs = createFs( | ||
{ | ||
'/src/components/404.astro': `<h1>Custom 404</h1>`, | ||
'/src/pages/page.astro': `<h1>Regular page</h1>`, | ||
}, | ||
root | ||
); | ||
|
||
await runInContainer( | ||
{ | ||
fs, | ||
root, | ||
userConfig: { | ||
output: 'server', | ||
integrations: [ | ||
{ | ||
name: '@astrojs/test-integration', | ||
hooks: { | ||
'astro:config:setup': ({ injectRoute }) => { | ||
injectRoute({ | ||
pattern: '/404', | ||
entryPoint: './src/components/404.astro', | ||
}); | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
async (container) => { | ||
{ | ||
// Regular pages are served as expected. | ||
const r = createRequestAndResponse({ method: 'GET', url: '/page' }); | ||
container.handle(r.req, r.res); | ||
await r.done; | ||
const doc = await r.text(); | ||
expect(doc).to.match(/<h1>Regular page<\/h1>/); | ||
expect(r.res.statusCode).to.equal(200); | ||
} | ||
{ | ||
// `/404` serves the custom 404 page as expected. | ||
const r = createRequestAndResponse({ method: 'GET', url: '/404' }); | ||
container.handle(r.req, r.res); | ||
await r.done; | ||
const doc = await r.text(); | ||
expect(doc).to.match(/<h1>Custom 404<\/h1>/); | ||
expect(r.res.statusCode).to.equal(200); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not quite sure whether we should expect a 404 status here or whether this request/response mock always returns 200? |
||
} | ||
{ | ||
// A non-existent page also serves the custom 404 page. | ||
const r = createRequestAndResponse({ method: 'GET', url: '/other-page' }); | ||
container.handle(r.req, r.res); | ||
await r.done; | ||
const doc = await r.text(); | ||
expect(doc).to.match(/<h1>Custom 404<\/h1>/); | ||
expect(r.res.statusCode).to.equal(200); | ||
} | ||
} | ||
); | ||
}); | ||
|
||
it('items in public/ are not available from root when using a base', async () => { | ||
await runInContainer( | ||
{ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is safe? Previously we only supported
404.astro
,404.md
,404.mdx
, and404.mdoc
as custom 404 routes. Those would all have aroute
of/404
or/404/
, so are still supported. This change adds custom 404 support forsrc/pages/404.html
or forinjectRoute({ pattern: '404', entrypoint: '...' })
in dev.