Commit 5a2182e6 authored by Brian Mathews's avatar Brian Mathews Committed by Joe Haddad

Add assetPrefix when fetching script and style dependencies (#9933)

* Add assetPrefix when fetching script and style dependencies for granularChunks

* Lint

* Fix assetPrefix usage with granularChunks

* Add tests for granularChunks with assetPrefix

* Cleanup
parent 32ec4f69
...@@ -69,7 +69,11 @@ export default class PageLoader { ...@@ -69,7 +69,11 @@ export default class PageLoader {
getDependencies(route) { getDependencies(route) {
return this.promisedBuildManifest.then( return this.promisedBuildManifest.then(
man => man =>
(man[route] && man[route].map(url => `/_next/${encodeURI(url)}`)) || [] (man[route] &&
man[route].map(
url => `${this.assetPrefix}/_next/${encodeURI(url)}`
)) ||
[]
) )
} }
...@@ -212,9 +216,9 @@ export default class PageLoader { ...@@ -212,9 +216,9 @@ export default class PageLoader {
if (cn.saveData || /2g/.test(cn.effectiveType)) return Promise.resolve() if (cn.saveData || /2g/.test(cn.effectiveType)) return Promise.resolve()
} }
let url = this.assetPrefix let url
if (isDependency) { if (isDependency) {
url += route url = route
} else { } else {
route = normalizeRoute(route) route = normalizeRoute(route)
this.prefetched[route] = true this.prefetched[route] = true
...@@ -224,7 +228,7 @@ export default class PageLoader { ...@@ -224,7 +228,7 @@ export default class PageLoader {
scriptRoute = scriptRoute.replace(/\.js$/, '.module.js') scriptRoute = scriptRoute.replace(/\.js$/, '.module.js')
} }
url += `/_next/static/${encodeURIComponent( url = `${this.assetPrefix}/_next/static/${encodeURIComponent(
this.buildId this.buildId
)}/pages${encodeURI(scriptRoute)}` )}/pages${encodeURI(scriptRoute)}`
} }
......
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer') const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
.BundleAnalyzerPlugin .BundleAnalyzerPlugin
module.exports = { module.exports = {
assetPrefix: '/foo/',
experimental: { experimental: {
granularChunks: true, granularChunks: true,
}, },
......
...@@ -5,7 +5,9 @@ const Page = () => { ...@@ -5,7 +5,9 @@ const Page = () => {
return ( return (
<div> <div>
<h2>Page3</h2> <h2>Page3</h2>
<Link href="/page2">Page2</Link> <Link href="/page2">
<a id="page2-link">Page2</a>
</Link>
</div> </div>
) )
} }
......
/* eslint-env jest */ /* eslint-env jest */
/* global jasmine */ /* global jasmine */
import { join } from 'path' import { join } from 'path'
import express from 'express'
import http from 'http'
import { import {
nextBuild, nextBuild,
findPort,
waitFor, waitFor,
nextStart, nextServer,
killApp, promiseCall,
stopApp,
} from 'next-test-utils' } from 'next-test-utils'
import { readdir, readFile, unlink, access } from 'fs-extra' import { readdir, readFile, unlink, access } from 'fs-extra'
import cheerio from 'cheerio' import cheerio from 'cheerio'
...@@ -107,18 +109,52 @@ describe('Chunking', () => { ...@@ -107,18 +109,52 @@ describe('Chunking', () => {
expect(misplacedReactDom).toBe(false) expect(misplacedReactDom).toBe(false)
}) })
it('should hydrate with granularChunks config', async () => { describe('Serving', () => {
const appPort = await findPort() let server
const app = await nextStart(appDir, appPort) let appPort
const browser = await webdriver(appPort, '/page2') beforeAll(async () => {
await waitFor(1000) await nextBuild(appDir)
const text = await browser.elementByCss('#padded-str').text() const app = nextServer({
dir: join(__dirname, '../'),
dev: false,
quiet: true,
})
const expressApp = express()
await app.prepare()
expressApp.use('/foo/_next', express.static(join(__dirname, '../.next')))
expressApp.use(app.getRequestHandler())
server = http.createServer(expressApp)
await promiseCall(server, 'listen')
appPort = server.address().port
})
expect(text).toBe('__rad__') afterAll(() => stopApp(server))
await browser.close() it('should hydrate with granularChunks config', async () => {
const browser = await webdriver(appPort, '/page2')
await waitFor(1000)
const text = await browser.elementByCss('#padded-str').text()
await killApp(app) expect(text).toBe('__rad__')
await browser.close()
})
it('should load chunks when navigating', async () => {
const browser = await webdriver(appPort, '/page3')
await waitFor(1000)
const text = await browser
.elementByCss('#page2-link')
.click()
.waitForElementByCss('#padded-str')
.elementByCss('#padded-str')
.text()
expect(text).toBe('__rad__')
await browser.close()
})
}) })
}) })
...@@ -271,7 +271,7 @@ export async function stopApp(server) { ...@@ -271,7 +271,7 @@ export async function stopApp(server) {
await promiseCall(server, 'close') await promiseCall(server, 'close')
} }
function promiseCall(obj, method, ...args) { export function promiseCall(obj, method, ...args) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const newArgs = [ const newArgs = [
...args, ...args,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment