Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline global CSS files in AMP pages in production builds #17914

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 47 additions & 17 deletions packages/next/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
} from '../next-server/server/get-page-files'
import { cleanAmpPath } from '../next-server/server/utils'
import { htmlEscapeJsonString } from '../server/htmlescape'
import { readFileSync } from 'fs'
import { join } from 'path'

export { DocumentContext, DocumentInitialProps, DocumentProps }

Expand Down Expand Up @@ -156,12 +158,8 @@ export class Head extends Component<

context!: React.ContextType<typeof DocumentComponentContext>

getCssLinks(files: DocumentFiles): JSX.Element[] | null {
const {
assetPrefix,
devOnlyCacheBusterQueryString,
dynamicImports,
} = this.context
getCssFiles(files: DocumentFiles): string[] {
const { dynamicImports } = this.context
const cssFiles = files.allFiles.filter((f) => f.endsWith('.css'))
const sharedFiles: Set<string> = new Set(files.sharedFiles)

Expand All @@ -180,6 +178,43 @@ export class Head extends Component<
cssFiles.push(...dynamicCssFiles)
}

return cssFiles
}

getAmpCss(
files: DocumentFiles,
curStyles: React.ReactElement[]
): JSX.Element | null {
const cssFromFiles = this.getCssFiles(files)
.map((file) => readFileSync(join('.next', file), { encoding: 'utf-8' }))
.join('')
.replace(/\/\*# sourceMappingURL=.*\*\//g, '')
.replace(/\/\*@ sourceURL=.*?\*\//g, '')
const cssFromElements = curStyles
.map((style) => style.props.dangerouslySetInnerHTML.__html)
.join('')
.replace(/\/\*# sourceMappingURL=.*\*\//g, '')
.replace(/\/\*@ sourceURL=.*?\*\//g, '')

if (cssFromFiles || cssFromElements) {
return (
<style
amp-custom=""
dangerouslySetInnerHTML={{
__html: `${cssFromFiles}${cssFromElements}`,
}}
/>
)
}

return null
}

getCssLinks(files: DocumentFiles): JSX.Element[] | null {
const { assetPrefix, devOnlyCacheBusterQueryString } = this.context
const cssFiles = this.getCssFiles(files)
const sharedFiles = new Set(files.sharedFiles)

const cssLinkElements: JSX.Element[] = []
cssFiles.forEach((file) => {
const isSharedFile = sharedFiles.has(file)
Expand Down Expand Up @@ -464,17 +499,12 @@ export class Head extends Component<
href="https://cdn.ampproject.org/v0.js"
/>
{/* Add custom styles before AMP styles to prevent accidental overrides */}
{styles && (
<style
amp-custom=""
dangerouslySetInnerHTML={{
__html: curStyles
.map((style) => style.props.dangerouslySetInnerHTML.__html)
.join('')
.replace(/\/\*# sourceMappingURL=.*\*\//g, '')
.replace(/\/\*@ sourceURL=.*?\*\//g, ''),
}}
/>
{this.getAmpCss(files, curStyles)}
{this.context.isDevelopment && (
// this element is used to mount development styles so the
// ordering matches production
// (by default, style-loader injects at the bottom of <head />)
<style amp-custom="" id="__next_css__DO_NOT_USE__" />
)}
<style
amp-boilerplate=""
Expand Down