mirror of
https://github.com/stack-auth/stack.git
synced 2026-06-04 21:04:37 +08:00
init deeplink redirects (#804)
<!--
Make sure you've read the CONTRIBUTING.md guidelines:
https://github.com/stack-auth/stack-auth/blob/dev/CONTRIBUTING.md
-->
Fix for a lot of the deep links that are causing 404 errors from old
docs.
<!-- ELLIPSIS_HIDDEN -->
----
> [!IMPORTANT]
> Adds dynamic redirection for documentation routes in multiple
sections, redirecting to the correct page or an overview, with special
handling for REST API routes.
>
> - **Behavior**:
> - Adds dynamic redirection for documentation routes in `js`, `next`,
`python`, `react`, and `rest-api` sections.
> - Redirects to the correct documentation page or an overview if the
page doesn't exist.
> - For `rest-api`, redirects to `/api` instead of `/docs`.
> - **Files**:
> - `route.ts` in `js`, `next`, `python`, `react`, and `rest-api`
sections handle the redirection logic.
> - Uses `source.getPage()` or `apiSource.getPage()` to check page
existence.
>
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=stack-auth%2Fstack-auth&utm_source=github&utm_medium=referral)<sup>
for 50c4b592a4. You can
[customize](https://app.ellipsis.dev/stack-auth/settings/summaries) this
summary. It will automatically update as commits are pushed.</sup>
----
<!-- ELLIPSIS_HIDDEN -->
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* Added dynamic redirection for documentation routes in JavaScript,
Next.js, Python, React, and REST API sections. Users are now
automatically redirected to the correct documentation page if it exists,
or receive a 404 not found response if the page is missing. This
improves navigation and error handling for documentation URLs.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Konsti Wohlwend <n2d4xc@gmail.com>
This commit is contained in:
parent
480dcba12e
commit
d601e99c6c
31
docs/src/app/js/[...path]/route.ts
Normal file
31
docs/src/app/js/[...path]/route.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { source } from 'lib/source';
|
||||
import { notFound, redirect } from 'next/navigation';
|
||||
import { NextRequest } from 'next/server';
|
||||
|
||||
export function GET(request: NextRequest) {
|
||||
const pathname = new URL(request.url).pathname;
|
||||
|
||||
// Ensure we have the correct target path without double prefixes using proper URL construction
|
||||
let targetPath: string;
|
||||
if (pathname.startsWith('/docs')) {
|
||||
targetPath = pathname;
|
||||
} else {
|
||||
// Remove leading slash and use as relative path to properly construct /docs prefix
|
||||
targetPath = new URL(pathname.substring(1), 'file:///docs/').pathname;
|
||||
}
|
||||
|
||||
// Extract slug by removing any '/docs' prefix and splitting by '/'
|
||||
const cleanPath = pathname.startsWith('/docs') ? pathname.substring(5) : pathname;
|
||||
const slug = cleanPath.substring(1).split('/').filter(Boolean);
|
||||
|
||||
// Check if the target page exists
|
||||
const page = source.getPage(slug);
|
||||
|
||||
if (page) {
|
||||
// Page exists, redirect to the full path
|
||||
return redirect(targetPath);
|
||||
} else {
|
||||
// Page doesn't exist, return 404
|
||||
return notFound();
|
||||
}
|
||||
}
|
||||
31
docs/src/app/next/[...path]/route.ts
Normal file
31
docs/src/app/next/[...path]/route.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { source } from 'lib/source';
|
||||
import { notFound, redirect } from 'next/navigation';
|
||||
import { NextRequest } from 'next/server';
|
||||
|
||||
export function GET(request: NextRequest) {
|
||||
const pathname = new URL(request.url).pathname;
|
||||
|
||||
// Ensure we have the correct target path without double prefixes using proper URL construction
|
||||
let targetPath: string;
|
||||
if (pathname.startsWith('/docs')) {
|
||||
targetPath = pathname;
|
||||
} else {
|
||||
// Remove leading slash and use as relative path to properly construct /docs prefix
|
||||
targetPath = new URL(pathname.substring(1), 'file:///docs/').pathname;
|
||||
}
|
||||
|
||||
// Extract slug by removing any '/docs' prefix and splitting by '/'
|
||||
const cleanPath = pathname.startsWith('/docs') ? pathname.substring(5) : pathname;
|
||||
const slug = cleanPath.substring(1).split('/').filter(Boolean);
|
||||
|
||||
// Check if the target page exists
|
||||
const page = source.getPage(slug);
|
||||
|
||||
if (page) {
|
||||
// Page exists, redirect to the full path
|
||||
return redirect(targetPath);
|
||||
} else {
|
||||
// Page doesn't exist, redirect to overview
|
||||
return notFound();
|
||||
}
|
||||
}
|
||||
31
docs/src/app/python/[...path]/route.ts
Normal file
31
docs/src/app/python/[...path]/route.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { source } from 'lib/source';
|
||||
import { notFound, redirect } from 'next/navigation';
|
||||
import { NextRequest } from 'next/server';
|
||||
|
||||
export function GET(request: NextRequest) {
|
||||
const pathname = new URL(request.url).pathname;
|
||||
|
||||
// Ensure we have the correct target path without double prefixes using proper URL construction
|
||||
let targetPath: string;
|
||||
if (pathname.startsWith('/docs')) {
|
||||
targetPath = pathname;
|
||||
} else {
|
||||
// Remove leading slash and use as relative path to properly construct /docs prefix
|
||||
targetPath = new URL(pathname.substring(1), 'file:///docs/').pathname;
|
||||
}
|
||||
|
||||
// Extract slug by removing any '/docs' prefix and splitting by '/'
|
||||
const cleanPath = pathname.startsWith('/docs') ? pathname.substring(5) : pathname;
|
||||
const slug = cleanPath.substring(1).split('/').filter(Boolean);
|
||||
|
||||
// Check if the target page exists
|
||||
const page = source.getPage(slug);
|
||||
|
||||
if (page) {
|
||||
// Page exists, redirect to the full path
|
||||
return redirect(targetPath);
|
||||
} else {
|
||||
// Page doesn't exist, redirect to overview
|
||||
return notFound();
|
||||
}
|
||||
}
|
||||
31
docs/src/app/react/[...path]/route.ts
Normal file
31
docs/src/app/react/[...path]/route.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { source } from 'lib/source';
|
||||
import { notFound, redirect } from 'next/navigation';
|
||||
import { NextRequest } from 'next/server';
|
||||
|
||||
export function GET(request: NextRequest) {
|
||||
const pathname = new URL(request.url).pathname;
|
||||
|
||||
// Ensure we have the correct target path without double prefixes using proper URL construction
|
||||
let targetPath: string;
|
||||
if (pathname.startsWith('/docs')) {
|
||||
targetPath = pathname;
|
||||
} else {
|
||||
// Remove leading slash and use as relative path to properly construct /docs prefix
|
||||
targetPath = new URL(pathname.substring(1), 'file:///docs/').pathname;
|
||||
}
|
||||
|
||||
// Extract slug by removing any '/docs' prefix and splitting by '/'
|
||||
const cleanPath = pathname.startsWith('/docs') ? pathname.substring(5) : pathname;
|
||||
const slug = cleanPath.substring(1).split('/').filter(Boolean);
|
||||
|
||||
// Check if the target page exists
|
||||
const page = source.getPage(slug);
|
||||
|
||||
if (page) {
|
||||
// Page exists, redirect to the full path
|
||||
return redirect(targetPath);
|
||||
} else {
|
||||
// Page doesn't exist, redirect to overview
|
||||
return notFound();
|
||||
}
|
||||
}
|
||||
31
docs/src/app/rest-api/[...path]/route.ts
Normal file
31
docs/src/app/rest-api/[...path]/route.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { apiSource } from 'lib/source';
|
||||
import { notFound, redirect } from 'next/navigation';
|
||||
import { NextRequest } from 'next/server';
|
||||
|
||||
export function GET(request: NextRequest) {
|
||||
const pathname = new URL(request.url).pathname;
|
||||
|
||||
// For rest-api, we redirect to /api not /docs using proper URL construction
|
||||
let targetPath: string;
|
||||
if (pathname.startsWith('/api')) {
|
||||
targetPath = pathname;
|
||||
} else {
|
||||
// Remove leading slash and use as relative path to properly construct /api prefix
|
||||
targetPath = new URL(pathname.substring(1), 'file:///api/').pathname;
|
||||
}
|
||||
|
||||
// Extract slug by removing any '/api' prefix and splitting by '/'
|
||||
const cleanPath = pathname.startsWith('/api') ? pathname.substring(4) : pathname;
|
||||
const slug = cleanPath.substring(1).split('/').filter(Boolean);
|
||||
|
||||
// Check if the target page exists using apiSource for API docs
|
||||
const page = apiSource.getPage(slug);
|
||||
|
||||
if (page) {
|
||||
// Page exists, redirect to the full path
|
||||
return redirect(targetPath);
|
||||
} else {
|
||||
// Page doesn't exist, redirect to overview
|
||||
return notFound();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user