Skip to content

Commit

Permalink
Ensure validator is included in vendored AMP validator (#70482)
Browse files Browse the repository at this point in the history
This aims to provide more stability so we aren't hitting the network to
grab the validator more than we need to and instead leverage the one
from build instead which should also reduce some test flakiness we've
been seeing.
  • Loading branch information
ijjk authored Sep 25, 2024
1 parent afb13c2 commit 98e5ee7
Show file tree
Hide file tree
Showing 21 changed files with 34 additions and 6,844 deletions.
6 changes: 6 additions & 0 deletions packages/next/src/export/routes/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ export async function exportPagesPage(
hybrid: components.pageConfig?.amp === 'hybrid',
}

if (!ampValidatorPath) {
ampValidatorPath = require.resolve(
'next/dist/compiled/amphtml-validator/validator_wasm.js'
)
}

const inAmpMode = isInAmpMode(ampState)
const hybridAmp = ampState.hybrid

Expand Down
10 changes: 7 additions & 3 deletions packages/next/src/server/dev/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,13 @@ export default class DevServer extends Server {
this.nextConfig.experimental?.amp?.skipValidation ?? false
this.renderOpts.ampValidator = (html: string, pathname: string) => {
const validatorPath =
this.nextConfig.experimental &&
this.nextConfig.experimental.amp &&
this.nextConfig.experimental.amp.validator
(this.nextConfig.experimental &&
this.nextConfig.experimental.amp &&
this.nextConfig.experimental.amp.validator) ||
require.resolve(
'next/dist/compiled/amphtml-validator/validator_wasm.js'
)

const AmpHtmlValidator =
require('next/dist/compiled/amphtml-validator') as typeof import('next/dist/compiled/amphtml-validator')
return AmpHtmlValidator.getInstance(validatorPath).then((validator) => {
Expand Down
15 changes: 15 additions & 0 deletions packages/next/taskfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2260,6 +2260,21 @@ export async function precompile(task, opts) {
['browser_polyfills', 'copy_ncced', 'copy_styled_jsx_assets'],
opts
)

const validatorRes = await fetch(
'https://cdn.ampproject.org/v0/validator_wasm.js'
)

if (!validatorRes.ok) {
throw new Error(
`Failed to get the AMP validator, status: ${validatorRes.status}`
)
}

await fs.writeFile(
join(__dirname, 'dist/compiled/amphtml-validator/validator_wasm.js'),
require('buffer').Buffer.from(await validatorRes.arrayBuffer())
)
}

// eslint-disable-next-line camelcase
Expand Down
2,254 changes: 0 additions & 2,254 deletions test/e2e/async-modules/amp-validator-wasm.js

This file was deleted.

8 changes: 1 addition & 7 deletions test/e2e/async-modules/next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
module.exports = {
experimental: {
amp: {
validator: require.resolve('./amp-validator-wasm.js'),
},
},
}
module.exports = {}
5 changes: 0 additions & 5 deletions test/integration/amp-export-validation/next.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
module.exports = {
output: 'export',
experimental: {
amp: {
validator: require.resolve('../../lib/amp-validator-wasm.js'),
},
},
// exportPathMap
}
1 change: 0 additions & 1 deletion test/integration/amphtml-custom-optimizer/next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module.exports = {
experimental: {
amp: {
validator: require.resolve('../../lib/amp-validator-wasm.js'),
optimizer: {
ampRuntimeVersion: '001515617716922',
rtv: true,
Expand Down
8 changes: 1 addition & 7 deletions test/integration/amphtml-custom-validator/next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
module.exports = {
experimental: {
amp: {
validator: require.resolve('../../lib/amp-validator-wasm.js'),
},
},
}
module.exports = {}
8 changes: 1 addition & 7 deletions test/integration/amphtml-fragment-style/next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
module.exports = {
experimental: {
amp: {
validator: require.resolve('../../lib/amp-validator-wasm.js'),
},
},
}
module.exports = {}
8 changes: 1 addition & 7 deletions test/integration/amphtml-ssg/next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
module.exports = {
experimental: {
amp: {
validator: require.resolve('../../lib/amp-validator-wasm.js'),
},
},
}
module.exports = {}
5 changes: 0 additions & 5 deletions test/integration/amphtml/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,5 @@ module.exports = {
amp: {
canonicalBase: 'http://localhost:1234',
},
experimental: {
amp: {
validator: require.resolve('../../lib/amp-validator-wasm.js'),
},
},
// edit here
}
5 changes: 0 additions & 5 deletions test/integration/auto-export-query-error/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,4 @@ module.exports = {
'/ssr': { page: '/ssr', query: { another: 'one' } },
}
},
experimental: {
amp: {
validator: require.resolve('../../lib/amp-validator-wasm.js'),
},
},
}
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
module.exports = {
experimental: {
amp: {
validator: require.resolve('../../../../lib/amp-validator-wasm.js'),
},
},
}
module.exports = {}
5 changes: 0 additions & 5 deletions test/integration/export-default-map/next.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
module.exports = {
output: 'export',
experimental: {
amp: {
validator: require.resolve('../../lib/amp-validator-wasm.js'),
},
},
}
5 changes: 0 additions & 5 deletions test/integration/i18n-support-base-path/next.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
module.exports = {
basePath: '/docs',
experimental: {
amp: {
validator: require.resolve('../../lib/amp-validator-wasm.js'),
},
},
i18n: {
// localeDetection: false,
locales: [
Expand Down
5 changes: 0 additions & 5 deletions test/integration/i18n-support/next.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
module.exports = {
// basePath: '/docs',
// trailingSlash: true,
experimental: {
amp: {
validator: require.resolve('../../lib/amp-validator-wasm.js'),
},
},
i18n: {
// localeDetection: false,
locales: [
Expand Down
8 changes: 1 addition & 7 deletions test/integration/page-config/next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
module.exports = {
experimental: {
amp: {
validator: require.resolve('../../lib/amp-validator-wasm.js'),
},
},
}
module.exports = {}
1 change: 0 additions & 1 deletion test/lib/amp-readme.md

This file was deleted.

2,254 changes: 0 additions & 2,254 deletions test/lib/amp-validator-wasm.js

This file was deleted.

2,254 changes: 0 additions & 2,254 deletions test/production/pages-dir/production/fixture/amp-validator-wasm.js

This file was deleted.

5 changes: 0 additions & 5 deletions test/production/pages-dir/production/fixture/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
setInterval(() => {}, 250)

module.exports = {
experimental: {
amp: {
validator: require.resolve('./amp-validator-wasm.js'),
},
},
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60,
Expand Down

0 comments on commit 98e5ee7

Please sign in to comment.