-
Notifications
You must be signed in to change notification settings - Fork 63
/
middleware.ts
65 lines (51 loc) · 2.39 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
58
59
60
61
62
63
64
65
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { getDynamicPageURL } from "@agility/nextjs/node"
// This function can be marked `async` if using `await` inside
export async function middleware(request: NextRequest) {
/*****************************
* *** AGILITY MIDDLEWARE ***
* 1: Check if this is a preview request,
* 2: Check if we are exiting preview
* 3: Check if this is a direct to a dynamic page
* based on a content id
*******************************/
const previewQ = request.nextUrl.searchParams.get("AgilityPreview")
let contentIDStr = request.nextUrl.searchParams.get("ContentID") as string || ""
if (request.nextUrl.searchParams.has("agilitypreviewkey")) {
//*** this is a preview request ***
const agilityPreviewKey = request.nextUrl.searchParams.get("agilitypreviewkey") || ""
//locale is also passed in the querystring on preview requests
const locale = request.nextUrl.searchParams.get("lang")
const slug = request.nextUrl.pathname
//valid preview key: we need to redirect to the correct url for preview
let redirectUrl = `${request.nextUrl.protocol}//${request.nextUrl.host}/api/preview?locale=${locale}&ContentID=${contentIDStr}&slug=${encodeURIComponent(slug)}&agilitypreviewkey=${encodeURIComponent(agilityPreviewKey)}`
return NextResponse.rewrite(redirectUrl)
} else if (previewQ === "0") {
//*** exit preview
const locale = request.nextUrl.searchParams.get("lang")
//we need to redirect to the correct url for preview
const slug = request.nextUrl.pathname
let redirectUrl = `${request.nextUrl.protocol}//${request.nextUrl.host}/api/preview/exit?locale=${locale}&ContentID=${contentIDStr}&slug=${encodeURIComponent(slug)}`
return NextResponse.redirect(redirectUrl)
} else if (contentIDStr) {
const contentID = parseInt(contentIDStr)
if (!isNaN(contentID) && contentID > 0) {
//*** this is a dynamic page request ***
let dynredirectUrl = `${request.nextUrl.protocol}//${request.nextUrl.host}/api/dynamic-redirect?ContentID=${contentID}`
return NextResponse.rewrite(dynredirectUrl)
}
}
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
'/((?!api|_next/static|_next/image|favicon.ico).*)',
],
}