File size: 4,336 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import type { ParsedUrlQuery } from 'querystring'
import type { Rewrite } from '../../../../lib/load-custom-routes'
import { getPathMatch } from './path-match'
import { matchHas, prepareDestination } from './prepare-destination'
import { removeTrailingSlash } from './remove-trailing-slash'
import { normalizeLocalePath } from '../../i18n/normalize-locale-path'
import { removeBasePath } from '../../../../client/remove-base-path'
import { parseRelativeUrl, type ParsedRelativeUrl } from './parse-relative-url'

interface ParsedAs extends Omit<ParsedRelativeUrl, 'slashes'> {
  slashes: boolean | undefined
}

export default function resolveRewrites(
  asPath: string,
  pages: string[],
  rewrites: {
    beforeFiles: Rewrite[]
    afterFiles: Rewrite[]
    fallback: Rewrite[]
  },
  query: ParsedUrlQuery,
  resolveHref: (path: string) => string,
  locales?: readonly string[]
): {
  matchedPage: boolean
  parsedAs: ParsedAs
  asPath: string
  resolvedHref?: string
  externalDest?: boolean
} {
  let matchedPage = false
  let externalDest = false
  let parsedAs: ParsedAs = parseRelativeUrl(asPath)
  let fsPathname = removeTrailingSlash(
    normalizeLocalePath(removeBasePath(parsedAs.pathname), locales).pathname
  )
  let resolvedHref

  const handleRewrite = (rewrite: Rewrite) => {
    const matcher = getPathMatch(
      rewrite.source + (process.env.__NEXT_TRAILING_SLASH ? '(/)?' : ''),
      {
        removeUnnamedParams: true,
        strict: true,
      }
    )

    let params = matcher(parsedAs.pathname)

    if ((rewrite.has || rewrite.missing) && params) {
      const hasParams = matchHas(
        {
          headers: {
            host: document.location.hostname,
            'user-agent': navigator.userAgent,
          },
          cookies: document.cookie
            .split('; ')
            .reduce<Record<string, string>>((acc, item) => {
              const [key, ...value] = item.split('=')
              acc[key] = value.join('=')
              return acc
            }, {}),
        } as any,
        parsedAs.query,
        rewrite.has,
        rewrite.missing
      )

      if (hasParams) {
        Object.assign(params, hasParams)
      } else {
        params = false
      }
    }

    if (params) {
      if (!rewrite.destination) {
        // this is a proxied rewrite which isn't handled on the client
        externalDest = true
        return true
      }
      const destRes = prepareDestination({
        appendParamsToQuery: true,
        destination: rewrite.destination,
        params: params,
        query: query,
      })
      parsedAs = destRes.parsedDestination
      asPath = destRes.newUrl
      Object.assign(query, destRes.parsedDestination.query)

      fsPathname = removeTrailingSlash(
        normalizeLocalePath(removeBasePath(asPath), locales).pathname
      )

      if (pages.includes(fsPathname)) {
        // check if we now match a page as this means we are done
        // resolving the rewrites
        matchedPage = true
        resolvedHref = fsPathname
        return true
      }

      // check if we match a dynamic-route, if so we break the rewrites chain
      resolvedHref = resolveHref(fsPathname)

      if (resolvedHref !== asPath && pages.includes(resolvedHref)) {
        matchedPage = true
        return true
      }
    }
  }
  let finished = false

  for (let i = 0; i < rewrites.beforeFiles.length; i++) {
    // we don't end after match in beforeFiles to allow
    // continuing through all beforeFiles rewrites
    handleRewrite(rewrites.beforeFiles[i])
  }
  matchedPage = pages.includes(fsPathname)

  if (!matchedPage) {
    if (!finished) {
      for (let i = 0; i < rewrites.afterFiles.length; i++) {
        if (handleRewrite(rewrites.afterFiles[i])) {
          finished = true
          break
        }
      }
    }

    // check dynamic route before processing fallback rewrites
    if (!finished) {
      resolvedHref = resolveHref(fsPathname)
      matchedPage = pages.includes(resolvedHref)
      finished = matchedPage
    }

    if (!finished) {
      for (let i = 0; i < rewrites.fallback.length; i++) {
        if (handleRewrite(rewrites.fallback[i])) {
          finished = true
          break
        }
      }
    }
  }

  return {
    asPath,
    parsedAs,
    matchedPage,
    resolvedHref,
    externalDest,
  }
}