-
Notifications
You must be signed in to change notification settings - Fork 88
fix: match edge runtime pages with optional trailing slash #1892
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { assertEquals } from 'https://deno.land/[email protected]/testing/asserts.ts' | ||
import { beforeEach, describe, it } from 'https://deno.land/[email protected]/testing/bdd.ts' | ||
import { updateModifiedHeaders, FetchEventResult } from './utils.ts' | ||
import { redirectTrailingSlash, updateModifiedHeaders } from './utils.ts' | ||
|
||
describe('updateModifiedHeaders', () => { | ||
it("does not modify the headers if 'x-middleware-override-headers' is not found", () => { | ||
|
@@ -62,3 +62,53 @@ describe('updateModifiedHeaders', () => { | |
}) | ||
}) | ||
}) | ||
|
||
describe('trailing slash redirects', () => { | ||
it('adds a trailing slash to the pathn if trailingSlash is enabled', () => { | ||
const url = new URL('https://example.com/foo') | ||
const result = redirectTrailingSlash(url, true) | ||
assertEquals(result?.status, 308) | ||
assertEquals(result?.headers.get('location'), 'https://example.com/foo/') | ||
}) | ||
|
||
it("doesn't add a trailing slash if trailingSlash is false", () => { | ||
const url = new URL('https://example.com/foo') | ||
const result = redirectTrailingSlash(url, false) | ||
assertEquals(result, undefined) | ||
}) | ||
|
||
it("doesn't add a trailing slash if the path is a file", () => { | ||
const url = new URL('https://example.com/foo.txt') | ||
const result = redirectTrailingSlash(url, true) | ||
assertEquals(result, undefined) | ||
}) | ||
it('adds a trailing slash if there is a dot in the path', () => { | ||
const url = new URL('https://example.com/foo.bar/baz') | ||
const result = redirectTrailingSlash(url, true) | ||
assertEquals(result?.status, 308) | ||
assertEquals(result?.headers.get('location'), 'https://example.com/foo.bar/baz/') | ||
}) | ||
it("doesn't add a trailing slash if the path is /", () => { | ||
const url = new URL('https://example.com/') | ||
const result = redirectTrailingSlash(url, true) | ||
assertEquals(result, undefined) | ||
}) | ||
it('removes a trailing slash from the path if trailingSlash is false', () => { | ||
const url = new URL('https://example.com/foo/') | ||
const result = redirectTrailingSlash(url, false) | ||
assertEquals(result?.status, 308) | ||
assertEquals(result?.headers.get('location'), 'https://example.com/foo') | ||
}) | ||
it("doesn't remove a trailing slash if trailingSlash is true", () => { | ||
const url = new URL('https://example.com/foo/') | ||
const result = redirectTrailingSlash(url, true) | ||
assertEquals(result, undefined) | ||
}) | ||
|
||
it('removes a trailing slash from the path if the path is a file', () => { | ||
const url = new URL('https://example.com/foo.txt/') | ||
const result = redirectTrailingSlash(url, false) | ||
assertEquals(result?.status, 308) | ||
assertEquals(result?.headers.get('location'), 'https://example.com/foo.txt') | ||
}) | ||
}) |
This file contains hidden or 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 hidden or 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,23 @@ | ||
import type { Context } from 'https://edge.netlify.com' | ||
// Available at build time | ||
import edgeFunction from './bundle.js' | ||
import { buildNextRequest, buildResponse, redirectTrailingSlash } from '../edge-shared/utils.ts' | ||
import nextConfig from '../edge-shared/nextConfig.json' assert { type: 'json' } | ||
|
||
const handler = async (req: Request, context: Context) => { | ||
const url = new URL(req.url) | ||
const redirect = redirectTrailingSlash(url, nextConfig.trailingSlash) | ||
if (redirect) { | ||
return redirect | ||
} | ||
const request = buildNextRequest(req, context, nextConfig) | ||
try { | ||
const result = await edgeFunction({ request }) | ||
return buildResponse({ result, request: req, context }) | ||
} catch (error) { | ||
console.error(error) | ||
return new Response(error.message, { status: 500 }) | ||
} | ||
} | ||
|
||
export default handler |
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've split this into a separate entrypoint, because functions don't need custom matcher handling or support for advanced middleware - but do want trailing slash handling unlike middleware