Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit c9212c4

Browse files
Enable optimized loading strategy (vercel#26021)
Enable optimized Loading strategy by default
1 parent 3c225b5 commit c9212c4

File tree

9 files changed

+15
-57
lines changed

9 files changed

+15
-57
lines changed

packages/next/next-server/lib/post-process.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,7 @@ class ImageOptimizerMiddleware implements PostProcessMiddleware {
191191
acc + `<link rel="preload" href="${imgHref}" as="image"/>`,
192192
''
193193
)
194-
return result.replace(
195-
/<link rel="preload"/,
196-
`${imagePreloadTags}<link rel="preload"`
197-
)
194+
return result.replace('<meta name="next-image-preload"/>', imagePreloadTags)
198195
}
199196
}
200197

packages/next/next-server/server/config-shared.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export const defaultConfig: NextConfig = {
113113
stats: false,
114114
externalDir: false,
115115
reactRoot: Number(process.env.NEXT_PRIVATE_REACT_ROOT) > 0,
116-
disableOptimizedLoading: true,
116+
disableOptimizedLoading: false,
117117
gzipSize: true,
118118
craCompat: false,
119119
},

packages/next/pages/_document.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,9 @@ export class Head extends Component<
705705
{!process.env.__NEXT_OPTIMIZE_CSS && (
706706
<noscript data-n-css={this.props.nonce ?? ''} />
707707
)}
708+
{process.env.__NEXT_OPTIMIZE_IMAGES && (
709+
<meta name="next-image-preload" />
710+
)}
708711
{!disableRuntimeJS &&
709712
!disableJsPreload &&
710713
this.getPreloadDynamicChunks()}

test/integration/app-document-import-order/test/index.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ describe('Root components import order', () => {
6161
const chunks = Array.from($('head').contents())
6262
.filter(
6363
(child) =>
64-
child.type === 'tag' &&
65-
child.name === 'link' &&
66-
child.attribs.href.match(requiredByRegex)
64+
child.type === 'script' &&
65+
child.name === 'script' &&
66+
child.attribs.src.match(requiredByRegex)
6767
)
68-
.map((child) => child.attribs.href.match(requiredByRegex)[1])
68+
.map((child) => child.attribs.src.match(requiredByRegex)[1])
6969

7070
const requiredByAppIndex = chunks.indexOf('requiredByApp')
7171
const requiredByPageIndex = chunks.indexOf('requiredByPage')

test/integration/basic/test/dynamic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export default (context, render) => {
219219
const $ = await get$('/dynamic/multiple-modules')
220220
const html = $('html').html()
221221
try {
222-
expect(html.match(/chunks[\\/]hello1\.js/g).length).toBe(2) // one for preload, one for the script tag
222+
expect(html.match(/chunks[\\/]hello1\.js/g).length).toBe(1)
223223
expect(html).not.toMatch(/hello2\.js/)
224224
} catch (err) {
225225
console.error(html)

test/integration/disable-js/test/index.test.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,7 @@ describe('disabled runtime JS', () => {
8383
const $ = cheerio.load(html)
8484
expect($('script#__NEXT_DATA__').length).toBe(1)
8585
})
86-
it('should have preload links', async () => {
87-
const html = await renderViaHTTP(appPort, '/')
88-
const $ = cheerio.load(html)
89-
expect($('link[rel=preload]').length).toBeGreaterThan(0)
90-
})
86+
9187
it('should have a script for each preload link', async () => {
9288
const html = await renderViaHTTP(appPort, '/')
9389
const $ = cheerio.load(html)

test/integration/dynamic-routing/test/index.test.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -952,20 +952,6 @@ function runTests(dev) {
952952
expect(res.status).toBe(400)
953953
})
954954

955-
it('should preload buildManifest for auto-export dynamic pages', async () => {
956-
const html = await renderViaHTTP(appPort, '/on-mount/hello')
957-
const $ = cheerio.load(html)
958-
let found = 0
959-
960-
for (const el of Array.from($('link[rel="preload"]'))) {
961-
const { href } = el.attribs
962-
if (href.includes('_buildManifest')) {
963-
found++
964-
}
965-
}
966-
expect(found).toBe(1)
967-
})
968-
969955
it('should not preload buildManifest for non-auto export dynamic pages', async () => {
970956
const html = await renderViaHTTP(appPort, '/hello')
971957
const $ = cheerio.load(html)

test/integration/production/test/index.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ describe('Production Usage', () => {
895895
}
896896
})
897897

898-
it('should have async on all script tags', async () => {
898+
it('should have defer on all script tags', async () => {
899899
const html = await renderViaHTTP(appPort, '/')
900900
const $ = cheerio.load(html)
901901
let missing = false
@@ -909,7 +909,7 @@ describe('Production Usage', () => {
909909
continue
910910
}
911911

912-
if (script.attribs.defer === '' || script.attribs.async !== '') {
912+
if (script.attribs.defer !== '' || script.attribs.async === '') {
913913
missing = true
914914
}
915915
}

test/integration/script-loader/test/index.test.js

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,14 @@ describe('Script Loader', () => {
4242

4343
async function test(id) {
4444
const script = await browser.elementById(id)
45-
const src = await script.getAttribute('src')
4645
const endScripts = await browser.elementsByCss(
4746
`#${id} ~ script[src^="/_next/static/"]`
4847
)
49-
const endPreloads = await browser.elementsByCss(
50-
`link[rel=preload][href="${src}"] ~ link[rel=preload][href^="/_next/static/"]`
51-
)
5248

5349
// Renders script tag
5450
expect(script).toBeDefined()
5551
// Script is inserted at the end
5652
expect(endScripts.length).toBe(0)
57-
//Preload is defined at the end
58-
expect(endPreloads.length).toBe(0)
5953
}
6054

6155
// afterInteractive script in page
@@ -102,31 +96,13 @@ describe('Script Loader', () => {
10296

10397
function test(id) {
10498
const script = $(`#${id}`)
105-
const src = script.attr('src')
10699

107100
// Renders script tag
108-
expect(script).toBeDefined()
109-
// Preload is inserted at the beginning
110-
expect(
111-
$(
112-
`link[rel=preload][href="${src}"] ~ link[rel=preload][href^="/_next/static/"]`
113-
).length &&
114-
!$(
115-
`link[rel=preload][href^="/_next/static/chunks/main"] ~ link[rel=preload][href="${src}"]`
116-
).length
117-
).toBeTruthy()
118-
119-
// Preload is inserted after fonts and CSS
120-
expect(
121-
$(
122-
`link[rel=stylesheet][href^="/_next/static/css"] ~ link[rel=preload][href="${src}"]`
123-
).length
124-
).toBeGreaterThan(0)
101+
expect(script.length).toBe(1)
125102

126103
// Script is inserted before NextScripts
127104
expect(
128-
$(`#__NEXT_DATA__ ~ #${id} ~ script[src^="/_next/static/chunks/main"]`)
129-
.length
105+
$(`#${id} ~ script[src^="/_next/static/chunks/main"]`).length
130106
).toBeGreaterThan(0)
131107
}
132108

0 commit comments

Comments
 (0)