-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Implement 404 redirect handling with middleware #16563
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
Open
dannyroosevelt
wants to merge
23
commits into
master
Choose a base branch
from
danny/404-redirect-handling
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
8216543
Update pnpm-lock.yaml
dannyroosevelt 7c06099
Fixing bad /apps redirect, adding catch-all redirect
dannyroosevelt f22bcbc
Update pnpm-lock.yaml
dannyroosevelt 4173b1a
Merge branch 'master' into danny/fixing-docs-apps-redirect
dannyroosevelt c9d3129
Merge branch 'master' into danny/fixing-docs-apps-redirect
dannyroosevelt a30008a
Escaping unsafe characters and pnpm lock
dannyroosevelt 354d379
Merge branch 'master' into danny/fixing-docs-apps-redirect
dannyroosevelt e4dca75
Update 404.tsx
dannyroosevelt 9b41e49
Merge branch 'danny/fixing-docs-apps-redirect' of github.com:Pipedrea…
dannyroosevelt 579e1c6
Merge branch 'danny/fixing-docs-apps-redirect' of github.com:Pipedrea…
dannyroosevelt 9600bde
Update pnpm-lock.yaml
dannyroosevelt 787970c
Delete apps.mdx
dannyroosevelt 9780aab
Updating header anchor tags
dannyroosevelt 36c08d5
Merge branch 'master' into danny/fixing-docs-apps-redirect
dannyroosevelt a7e2163
Update users.mdx
dannyroosevelt 2326e48
Merge branch 'danny/fixing-docs-apps-redirect' of github.com:Pipedrea…
dannyroosevelt 24ef2b6
Modifying 404 handling
dannyroosevelt a260ae4
Add redirects for /apps/apps/ and /integrations/ paths
dannyroosevelt bfd8748
Linting
dannyroosevelt 4b972dd
Add 404 redirect handling with middleware
dannyroosevelt 0e9a3f3
Update middleware.ts
dannyroosevelt 40003d3
Merge branch 'master' into danny/404-redirect-handling
dannyroosevelt c8e00d0
Update users.mdx
dannyroosevelt 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { NextResponse } from "next/server" | ||
import type { NextRequest } from "next/server" | ||
|
||
/** | ||
* Middleware to handle 404s by redirecting to the home page | ||
* Instead of showing a 404 error page, we redirect to the root | ||
* Using a 301 (permanent) redirect for better SEO handling | ||
*/ | ||
export function middleware(request: NextRequest) { | ||
// We only want to handle 404s, not other pages | ||
// This check isn't foolproof but helps avoid redirecting existing pages | ||
const pathname = request.nextUrl.pathname | ||
|
||
// Check if this is a static asset or API route - we don't want to redirect these | ||
if ( | ||
pathname.startsWith("/_next") || | ||
pathname.startsWith("/api/") || | ||
pathname.startsWith("/images/") || | ||
pathname.includes(".") // Likely a file, e.g. favicon.ico | ||
) { | ||
return NextResponse.next() | ||
} | ||
|
||
// Return a 301 (permanent) redirect to the home page | ||
return NextResponse.redirect(new URL("/", request.url), 301) | ||
} | ||
|
||
// Configure which paths this middleware will run on | ||
// This runs the middleware on paths that don't exist in our app | ||
export const config = { | ||
matcher: [ | ||
/* | ||
* Match all request paths except for: | ||
* 1. Existing pages in the pages directory | ||
* 2. API routes | ||
* 3. Static files (images, etc) | ||
* 4. System paths like _next, favicon.ico, etc. | ||
*/ | ||
"/((?!_next|api|images|favicon.ico).*)", | ||
], | ||
} | ||
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,37 @@ | ||
import { useEffect } from "react" | ||
import { useRouter } from "next/router" | ||
|
||
/** | ||
* Custom 404 component - this is a fallback in case the middleware redirect doesn't work | ||
* The middleware.ts file handles the main redirect logic with a 301 status code | ||
* This component will only be shown if the middleware fails to redirect | ||
*/ | ||
export default function Custom404() { | ||
const router = useRouter() | ||
|
||
useEffect(() => { | ||
// Fallback redirect if middleware didn't handle it | ||
// Using a short timeout to ensure middleware has a chance to run first | ||
const redirectTimeout = setTimeout(() => { | ||
router.replace("/") | ||
}, 100) | ||
|
||
return () => clearTimeout(redirectTimeout) | ||
}, [ | ||
router, | ||
]) | ||
|
||
return ( | ||
<div style={{ | ||
display: "flex", | ||
justifyContent: "center", | ||
alignItems: "center", | ||
height: "100vh", | ||
}}> | ||
<div> | ||
<h1>Page not found</h1> | ||
<p>Redirecting to home page...</p> | ||
</div> | ||
</div> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.