-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create rewrites for redirect loop (#1787)
Creates rewrites for redirect loop, as case sensitivity in Next.js is inconsistent (see also vercel/next.js#21498 (comment)). --- Background: Sometimes you want a redirect from an uppercase to a lowercase URL (e.g. `/Example` to `/example`) for SEO purposes. This can be created in our admin without problems. The casing is saved correctly in the DB and transferred to Next. The problem is that this creates a redirect loop in the site. The reason is that the case sensitivity of Next.js is inconsistent. The delivery of pages is case sensitive. The redirects are case insensitive: Without redirect: http://localhost:3000/example -> page is delivered http://localhost:3000/Example -> 404 With redirect: http://localhost:3000/example -> Redirect to /example -> Loop http://localhost:3000/Example -> Redirect to /example -> Loop Also described here: vercel/next.js#21498 --------- Co-authored-by: Thomas Dax <[email protected]>
- Loading branch information
1 parent
ddd798e
commit a6cd016
Showing
3 changed files
with
121 additions
and
60 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
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,27 @@ | ||
import { Rewrite } from "next/dist/lib/load-custom-routes"; | ||
|
||
import { getRedirects } from "./createRedirects"; | ||
|
||
const createRewrites = async () => { | ||
const apiUrl = process.env.API_URL_INTERNAL; | ||
if (!apiUrl) { | ||
console.error("No Environment Variable API_URL_INTERNAL available. Can not perform redirect config"); | ||
return { redirects: [], rewrites: [] }; | ||
} | ||
|
||
const rewrites: Rewrite[] = []; | ||
|
||
for await (const redirect of getRedirects()) { | ||
const { source, destination } = redirect; | ||
|
||
// A rewrite is created for each redirect where the source and destination differ only by casing (otherwise, this causes a redirection loop). | ||
// For instance, a rewrite is created for the redirect /Example -> /example. | ||
if (source && destination && source.toLowerCase() === destination.toLowerCase()) { | ||
rewrites.push({ source, destination }); | ||
} | ||
} | ||
|
||
return rewrites; | ||
}; | ||
|
||
export { createRewrites }; |