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 301 redirect to trailing slash when not specified in createPage.path #19567
Closed
adrienharnay
wants to merge
6
commits into
gatsbyjs:master
from
adrienharnay:topics/fix-redirect-trailing-slash
Closed
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 |
---|---|---|
|
@@ -85,9 +85,18 @@ module.exports = async program => { | |
app.use(telemetry.expressMiddleware(`SERVE`)) | ||
|
||
router.use(compression()) | ||
router.use(express.static(`public`)) | ||
router.use(express.static(`public`, { redirect: false })) | ||
const matchPaths = await readMatchPaths(program) | ||
router.use(matchPathRouter(matchPaths, { root })) | ||
router.use((req, res, next) => { | ||
if (!/.+\..+$/g.test(req.url) && req.url.slice(-1) !== `/`) { | ||
res.sendFile(req.url + `/index.html`, { | ||
root, | ||
}) | ||
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. What if the file doesn't exist? A callback argument can be added here to deal with this case and allow for the regular 404 logic to occur:
|
||
} else { | ||
next() | ||
} | ||
}) | ||
router.use((req, res, next) => { | ||
if (req.accepts(`html`)) { | ||
return res.status(404).sendFile(`404.html`, { root }) | ||
|
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.
for clarity, could we store this regex in a variable that has a descriptive name? mentally grokking this regex is a bit confounding 😆
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.
or at least add a comment above explaining what this is doing, what it's solving, etc. I'm just worried about the long term maintenance of this fix getting lost in 6-12m
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.
this is copied from my examples, and this is probably not good test to have in retrospect (it infer that resource should be file if it has
.
in it, but that's true at all), so we should remove at least first part of condition leaving only test for trailing slash.I wonder what we will get now for legitimate 404s tho, now that
serve-static
won't immediately return 404