Skip to content

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
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8216543
Update pnpm-lock.yaml
dannyroosevelt Apr 29, 2025
7c06099
Fixing bad /apps redirect, adding catch-all redirect
dannyroosevelt Apr 30, 2025
f22bcbc
Update pnpm-lock.yaml
dannyroosevelt Apr 30, 2025
4173b1a
Merge branch 'master' into danny/fixing-docs-apps-redirect
dannyroosevelt Apr 30, 2025
c9d3129
Merge branch 'master' into danny/fixing-docs-apps-redirect
dannyroosevelt Apr 30, 2025
a30008a
Escaping unsafe characters and pnpm lock
dannyroosevelt Apr 30, 2025
354d379
Merge branch 'master' into danny/fixing-docs-apps-redirect
dannyroosevelt Apr 30, 2025
e4dca75
Update 404.tsx
dannyroosevelt Apr 30, 2025
9b41e49
Merge branch 'danny/fixing-docs-apps-redirect' of github.com:Pipedrea…
dannyroosevelt Apr 30, 2025
579e1c6
Merge branch 'danny/fixing-docs-apps-redirect' of github.com:Pipedrea…
dannyroosevelt Apr 30, 2025
9600bde
Update pnpm-lock.yaml
dannyroosevelt Apr 30, 2025
787970c
Delete apps.mdx
dannyroosevelt Apr 30, 2025
9780aab
Updating header anchor tags
dannyroosevelt May 6, 2025
36c08d5
Merge branch 'master' into danny/fixing-docs-apps-redirect
dannyroosevelt May 6, 2025
a7e2163
Update users.mdx
dannyroosevelt May 6, 2025
2326e48
Merge branch 'danny/fixing-docs-apps-redirect' of github.com:Pipedrea…
dannyroosevelt May 6, 2025
24ef2b6
Modifying 404 handling
dannyroosevelt May 6, 2025
a260ae4
Add redirects for /apps/apps/ and /integrations/ paths
dannyroosevelt May 6, 2025
bfd8748
Linting
dannyroosevelt May 6, 2025
4b972dd
Add 404 redirect handling with middleware
dannyroosevelt May 6, 2025
0e9a3f3
Update middleware.ts
dannyroosevelt May 6, 2025
40003d3
Merge branch 'master' into danny/404-redirect-handling
dannyroosevelt May 6, 2025
c8e00d0
Update users.mdx
dannyroosevelt May 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions docs-v2/middleware.ts
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).*)",
],
}
37 changes: 37 additions & 0 deletions docs-v2/pages/404.tsx
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>
)
}
1 change: 0 additions & 1 deletion docs-v2/pages/connect/api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2797,7 +2797,6 @@ curl -X DELETE \

Pipedream returns a `204 No Content` response on successful deletion


#### Retrieve Events Emitted by Deployed Trigger

Retrieve a list of the last events that a deployed trigger emitted.
Expand Down
2 changes: 1 addition & 1 deletion docs-v2/pages/connect/managed-auth/users.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ curl -X DELETE "https://api.pipedream.com/v1/connect/{project_id}/users/{externa
-H "Authorization: Bearer {access_token}"
```

For complete API details including TypeScript and Node.js examples, see the [API reference](/connect/api/#delete-end-user).
For complete API details including TypeScript and Node.js examples, see the [API reference](/connect/api/#delete-external-user).
Loading