Skip to content

Commit

Permalink
Allow for NextURL to parse prefetch requests
Browse files Browse the repository at this point in the history
  • Loading branch information
javivelasco committed May 24, 2022
1 parent 5696f9f commit 9d88f8f
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 6 deletions.
28 changes: 22 additions & 6 deletions packages/next/server/web/next-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class NextURL {
url: URL
options: Options
basePath: string
buildId?: string
locale?: {
defaultLocale: string
domain?: DomainLocale
Expand Down Expand Up @@ -61,6 +62,16 @@ export class NextURL {
private analyzeUrl() {
const { headers = {}, basePath, i18n } = this[Internal].options

if (this[Internal].url.pathname.startsWith('/_next/data/')) {
const [buildId, ...rest] = this[Internal].url.pathname
.replace(/^\/_next\/data\//, '')
.replace(/\.json$/, '')
.split('/')
this[Internal].buildId = buildId
this[Internal].url.pathname =
rest[0] !== 'index' ? `/${rest.join('/')}` : '/'
}

if (basePath && this[Internal].url.pathname.startsWith(basePath)) {
this[Internal].url.pathname = replaceBasePath(
this[Internal].url.pathname,
Expand Down Expand Up @@ -102,17 +113,22 @@ export class NextURL {

if (
this[Internal].locale?.locale &&
i18n?.defaultLocale !== this[Internal].locale?.locale &&
!this.hasPathPrefix('/api')
((i18n?.defaultLocale !== this[Internal].locale?.locale &&
!this.hasPathPrefix('/api')) ||
this[Internal].buildId)
) {
pathname = `/${this[Internal].locale?.locale}${pathname}`
}

if (this[Internal].basePath) {
pathname = `${this[Internal].basePath}${pathname}`
}
return this[Internal].buildId
? `/_next/data/${this[Internal].buildId}${
pathname === '/' ? '/index' : pathname
}.json`
: `${this[Internal].basePath}${pathname}`
}

return pathname
public get buildId() {
return this[Internal].buildId
}

private hasPathPrefix(prefix: string) {
Expand Down
69 changes: 69 additions & 0 deletions test/unit/web-runtime/next-url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,72 @@ it('does not add locale for api route', () => {
expect(url.toString()).toEqual(expected)
expect(url.toJSON()).toEqual(expected)
})

it('correctly parses a prefetch url', async () => {
const url = new NextURL(
'/_next/data/1234/en/hello.json',
'http://127.0.0.1:3000'
)
expect(url.buildId).toEqual('1234')
expect(url.pathname).toEqual('/en/hello')
expect(url.locale).toEqual('')
expect(String(url)).toEqual(
'http://localhost:3000/_next/data/1234/en/hello.json'
)
})

it('correctly parses a prefetch index url', async () => {
const url = new NextURL(
'/_next/data/development/index.json',
'http://127.0.0.1:3000'
)
expect(url.pathname).toEqual('/')
})

it('correctly parses a prefetch url with i18n', async () => {
const url = new NextURL(
'/_next/data/development/en/hello.json',
'http://127.0.0.1:3000',
{
i18n: {
defaultLocale: 'en',
locales: ['en', 'es', 'fr'],
},
}
)
expect(url.buildId).toEqual('development')
expect(url.pathname).toEqual('/hello')
expect(url.locale).toEqual('en')
expect(String(url)).toEqual(
'http://localhost:3000/_next/data/development/en/hello.json'
)
})

it('allows to update the pathname for a prefetch url', async () => {
const url = new NextURL(
'/_next/data/development/en/hello.json',
'http://127.0.0.1:3000',
{
i18n: {
defaultLocale: 'en',
locales: ['en', 'es', 'fr'],
},
}
)

url.pathname = '/foo'
expect(String(url)).toEqual(
'http://localhost:3000/_next/data/development/en/foo.json'
)
})

it('allows to update the pathname to the root path for a prefetch url', async () => {
const url = new NextURL(
'/_next/data/development/hello.json',
'http://127.0.0.1:3000'
)
url.pathname = '/'
expect(String(url)).toEqual(
'http://localhost:3000/_next/data/development/index.json'
)
})

0 comments on commit 9d88f8f

Please sign in to comment.