Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom amp optimizer and skip validation mode (#10705)
* Add custom amp optimizer and skip validation mode * fix: type * fix worker lint errors
- Loading branch information
1 parent
1690a75
commit aadb31f
Showing
10 changed files
with
116 additions
and
7 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
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
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
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,10 +1,13 @@ | ||
export default async function optimize(html: string): Promise<string> { | ||
export default async function optimize( | ||
html: string, | ||
config: any | ||
): Promise<string> { | ||
let AmpOptimizer | ||
try { | ||
AmpOptimizer = require('@ampproject/toolbox-optimizer') | ||
} catch (_) { | ||
return html | ||
} | ||
const optimizer = AmpOptimizer.create() | ||
return optimizer.transformHtml(html) | ||
const optimizer = AmpOptimizer.create(config) | ||
return optimizer.transformHtml(html, config) | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module.exports = { | ||
experimental: { | ||
amp: { | ||
optimizer: { | ||
ampRuntimeVersion: '001515617716922', | ||
ampUrlPrefix: '/amp', | ||
verbose: true, | ||
}, | ||
skipValidation: true, | ||
}, | ||
}, | ||
} |
20 changes: 20 additions & 0 deletions
20
test/integration/amphtml-custom-optimizer/pages/dynamic.js
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export const config = { | ||
amp: true, | ||
} | ||
|
||
const Dynamic = props => ( | ||
<amp-img | ||
width="500" | ||
height="500" | ||
layout="responsive" | ||
src={props.src} | ||
></amp-img> | ||
) | ||
|
||
Dynamic.getInitialProps = () => { | ||
return { | ||
src: 'https://amp.dev/static/samples/img/story_dog2_portrait.jpg', | ||
} | ||
} | ||
|
||
export default Dynamic |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const config = { | ||
amp: true, | ||
} | ||
|
||
export default () => ( | ||
<amp-twitter | ||
width="500" | ||
height="500" | ||
layout="responsive" | ||
data-tweetid="1159145442896166912" | ||
></amp-twitter> | ||
) |
52 changes: 52 additions & 0 deletions
52
test/integration/amphtml-custom-optimizer/test/index.test.js
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* eslint-env jest */ | ||
/* global jasmine */ | ||
import { join } from 'path' | ||
import { | ||
nextBuild, | ||
findPort, | ||
nextStart, | ||
killApp, | ||
renderViaHTTP, | ||
} from 'next-test-utils' | ||
|
||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 1 | ||
|
||
let app | ||
let appPort | ||
const appDir = join(__dirname, '../') | ||
|
||
describe('AMP Custom Optimizer', () => { | ||
it('should build and start for static page', async () => { | ||
const { code } = await nextBuild(appDir, undefined) | ||
expect(code).toBe(0) | ||
|
||
appPort = await findPort() | ||
app = await nextStart(appDir, appPort) | ||
|
||
const html = await renderViaHTTP(appPort, '/') | ||
await killApp(app) | ||
|
||
expect(html).toContain( | ||
'amp-twitter width="500" height="500" layout="responsive" data-tweetid="1159145442896166912"' | ||
) | ||
expect(html).toContain('i-amphtml-version="001515617716922"') | ||
expect(html).toContain('script async src="/amp/rtv/001515617716922/v0.js"') | ||
}) | ||
|
||
it('should build and start for dynamic page', async () => { | ||
const { code } = await nextBuild(appDir, undefined) | ||
expect(code).toBe(0) | ||
|
||
appPort = await findPort() | ||
app = await nextStart(appDir, appPort) | ||
|
||
const html = await renderViaHTTP(appPort, '/dynamic') | ||
await killApp(app) | ||
|
||
expect(html).toContain( | ||
'amp-img width="500" height="500" layout="responsive" src="https://amp.dev/static/samples/img/story_dog2_portrait.jpg"' | ||
) | ||
expect(html).toContain('i-amphtml-version="001515617716922"') | ||
expect(html).toContain('script async src="/amp/rtv/001515617716922/v0.js"') | ||
}) | ||
}) |