-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
180 additions
and
320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 4 additions & 46 deletions
50
packages/next/src/server/future/normalizers/request/base-path.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,11 @@ | ||
import { BasePathPathnameNormalizer } from './base-path' | ||
|
||
describe('BasePathPathnameNormalizer', () => { | ||
describe('match', () => { | ||
it('should return false if there is no basePath', () => { | ||
let normalizer = new BasePathPathnameNormalizer('') | ||
expect(normalizer.match('/')).toBe(false) | ||
normalizer = new BasePathPathnameNormalizer('/') | ||
expect(normalizer.match('/')).toBe(false) | ||
}) | ||
|
||
it('should return false if the pathname does not start with the basePath', () => { | ||
const normalizer = new BasePathPathnameNormalizer('/foo') | ||
const pathnames = ['/bar', '/bar/foo', '/fooo/bar'] | ||
for (const pathname of pathnames) { | ||
expect(normalizer.match(pathname)).toBe(false) | ||
} | ||
}) | ||
|
||
it('should return true if the pathname starts with the basePath', () => { | ||
const normalizer = new BasePathPathnameNormalizer('/foo') | ||
const pathnames = ['/foo', '/foo/bar', '/foo/bar/baz'] | ||
for (const pathname of pathnames) { | ||
expect(normalizer.match(pathname)).toBe(true) | ||
} | ||
}) | ||
it('should throw when provided with a blank basePath', () => { | ||
expect(() => new BasePathPathnameNormalizer('')).toThrow() | ||
}) | ||
|
||
describe('normalize', () => { | ||
it('should return the same pathname if there is no basePath', () => { | ||
let normalizer = new BasePathPathnameNormalizer('') | ||
expect(normalizer.normalize('/foo')).toBe('/foo') | ||
normalizer = new BasePathPathnameNormalizer('/') | ||
expect(normalizer.normalize('/foo')).toBe('/foo') | ||
}) | ||
|
||
it('should return the same pathname if we are not matched and the pathname does not start with the basePath', () => { | ||
const normalizer = new BasePathPathnameNormalizer('/foo') | ||
let pathnames = ['/bar', '/bar/foo', '/fooo/bar'] | ||
for (const pathname of pathnames) { | ||
expect(normalizer.normalize(pathname)).toBe(pathname) | ||
} | ||
}) | ||
|
||
it('should strip the basePath from the pathname when it matches', () => { | ||
const normalizer = new BasePathPathnameNormalizer('/foo') | ||
const pathnames = ['/foo', '/foo/bar', '/foo/bar/baz'] | ||
for (const pathname of pathnames) { | ||
expect(normalizer.normalize(pathname)).toBe(pathname.substring(4)) | ||
} | ||
}) | ||
it('should throw when provided with a basePath of "/"', () => { | ||
expect(() => new BasePathPathnameNormalizer('/')).toThrow() | ||
}) | ||
}) |
36 changes: 10 additions & 26 deletions
36
packages/next/src/server/future/normalizers/request/base-path.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,16 @@ | ||
import type { PathnameNormalizer } from './pathname-normalizer' | ||
|
||
export class BasePathPathnameNormalizer implements PathnameNormalizer { | ||
private readonly basePath?: string | ||
constructor(basePath: string) { | ||
// A basePath of `/` is not a basePath. | ||
if (!basePath || basePath === '/') return | ||
|
||
this.basePath = basePath | ||
} | ||
|
||
public match(pathname: string) { | ||
// If there's no basePath, we don't match. | ||
if (!this.basePath) return false | ||
|
||
// If the pathname doesn't start with the basePath, we don't match. | ||
if (pathname !== this.basePath && !pathname.startsWith(this.basePath + '/')) | ||
return false | ||
import { PrefixPathnameNormalizer } from './prefix' | ||
|
||
return true | ||
} | ||
|
||
public normalize(pathname: string, matched?: boolean): string { | ||
// If there's no basePath, we don't need to normalize. | ||
if (!this.basePath) return pathname | ||
|
||
// If we're not matched and we don't match, we don't need to normalize. | ||
if (!matched && !this.match(pathname)) return pathname | ||
export class BasePathPathnameNormalizer | ||
extends PrefixPathnameNormalizer | ||
implements PathnameNormalizer | ||
{ | ||
constructor(basePath: string) { | ||
if (!basePath || basePath === '/') { | ||
throw new Error('Invariant: basePath must be set and cannot be "/"') | ||
} | ||
|
||
return pathname.substring(this.basePath.length) | ||
super(basePath) | ||
} | ||
} |
31 changes: 11 additions & 20 deletions
31
packages/next/src/server/future/normalizers/request/next-data.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,31 @@ | ||
import type { PathnameNormalizer } from './pathname-normalizer' | ||
|
||
import { denormalizePagePath } from '../../../../shared/lib/page-path/denormalize-page-path' | ||
import { PrefixPathnameNormalizer } from './prefix' | ||
import { SuffixPathnameNormalizer } from './suffix' | ||
|
||
export class NextDataPathnameNormalizer implements PathnameNormalizer { | ||
private readonly prefix: string | ||
private readonly prefix: PrefixPathnameNormalizer | ||
private readonly suffix = new SuffixPathnameNormalizer('.json') | ||
constructor(buildID: string) { | ||
if (!buildID) { | ||
throw new Error('Invariant: buildID is required') | ||
} | ||
|
||
this.prefix = `/_next/data/${buildID}` | ||
this.prefix = new PrefixPathnameNormalizer(`/_next/data/${buildID}`) | ||
} | ||
|
||
public match(pathname: string) { | ||
// If the pathname doesn't start with the prefix, we don't match. | ||
if (!pathname.startsWith(`${this.prefix}/`)) return false | ||
|
||
// If the pathname ends with `.json`, we don't match. | ||
if (!pathname.endsWith('.json')) return false | ||
|
||
return true | ||
return this.prefix.match(pathname) && this.suffix.match(pathname) | ||
} | ||
|
||
public normalize(pathname: string, matched?: boolean): string { | ||
// If we're not matched and we don't match, we don't need to normalize. | ||
if (!matched && !this.match(pathname)) return pathname | ||
|
||
// Remove the prefix and the `.json` extension. | ||
pathname = pathname.substring( | ||
this.prefix.length, | ||
pathname.length - '.json'.length | ||
) | ||
|
||
// If the pathname is `/index`, we normalize it to `/`. | ||
if (pathname === '/index') { | ||
return '/' | ||
} | ||
pathname = this.prefix.normalize(pathname, true) | ||
pathname = this.suffix.normalize(pathname, true) | ||
|
||
return pathname | ||
return denormalizePagePath(pathname) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.