-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathmiddleware.ts
57 lines (53 loc) · 1.65 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { auth } from './src/auth';
import { NextRequest, NextResponse } from 'next/server';
import type { NextApiRequest, NextApiResponse } from 'next';
import {
PATH_ADMIN,
PATH_ADMIN_PHOTOS,
PATH_OG,
PATH_OG_SAMPLE,
PREFIX_PHOTO,
PREFIX_TAG,
} from './src/app/paths';
export default function middleware(req: NextRequest, res:NextResponse) {
const pathname = req.nextUrl.pathname;
if (pathname === PATH_ADMIN) {
return NextResponse.redirect(new URL(PATH_ADMIN_PHOTOS, req.url));
} else if (pathname === PATH_OG) {
return NextResponse.redirect(new URL(PATH_OG_SAMPLE, req.url));
} else if (/^\/photos\/(.)+$/.test(pathname)) {
// Accept /photos/* paths, but serve /p/*
const matches = pathname.match(/^\/photos\/(.+)$/);
return NextResponse.rewrite(new URL(
`${PREFIX_PHOTO}/${matches?.[1]}`,
req.url,
));
} else if (/^\/t\/(.)+$/.test(pathname)) {
// Accept /t/* paths, but serve /tag/*
const matches = pathname.match(/^\/t\/(.+)$/);
return NextResponse.rewrite(new URL(
`${PREFIX_TAG}/${matches?.[1]}`,
req.url,
));
}
return auth(
req as unknown as NextApiRequest,
res as unknown as NextApiResponse,
);
}
export const config = {
// Excludes:
// - /api + /api/auth*
// - /_next/static*
// - /_next/image*
// - /favicon.ico + /favicons/*
// - /grid
// - /feed
// - / (root)
// - /home-image
// - /template-image
// - /template-image-tight
// - /template-url
// eslint-disable-next-line max-len
matcher: ['/((?!api$|api/auth|_next/static|_next/image|favicon.ico$|favicons/|grid$|feed$|home-image$|template-image$|template-image-tight$|template-url$|$).*)'],
};