CSS Modules - Support AMP and Lite rendering#13992
Conversation
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
…opilot] Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
There was a problem hiding this comment.
Pull request overview
Adds an AMP/Lite CSS inlining mechanism for the Next.js app so that CSS Modules styles are available during SSR (where external stylesheets can’t be used), covering both dev and production flows.
Changes:
- Introduces
getAmpLiteCssto inline CSS for AMP/Lite by reading from Next manifests in production and a dev-only extracted CSS file in development. - Adds a dev-only webpack loader (
DevCssExtractLoader) to extract css-loader output tobuild/dev-css-modules.css, and injects it vianext.config.js. - Wires AMP/Lite render paths in
_document.page.tsxto append extracted CSS, plus adds supporting tests, docs, and log codes.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| ws-nextjs-app/utilities/getAmpLiteCss/index.ts | Implements AMP/Lite CSS resolution and inlining (dev + prod) using manifests / extracted file. |
| ws-nextjs-app/utilities/getAmpLiteCss/index.test.ts | Unit tests for the AMP/Lite CSS resolution, manifest parsing, and error handling. |
| ws-nextjs-app/scripts/DevCssExtractLoader.cjs | Custom dev-only webpack loader to extract hashed CSS Modules CSS from css-loader output. |
| ws-nextjs-app/scripts/DevCssExtractLoader.test.ts | Unit tests validating loader extraction behavior and disk writes. |
| ws-nextjs-app/pages/_document.page.tsx | Appends extracted CSS to AMP and Lite render branches using __NEXT_DATA__. |
| ws-nextjs-app/next.config.js | Injects the dev CSS extraction loader before css-loader in dev builds. |
| ws-nextjs-app/docs/amp-lite-css-extraction.md | Documents rationale and approach for AMP/Lite CSS extraction in dev and prod. |
| src/app/lib/logger.const.js | Adds new log codes for manifest/CSS extraction read errors. |
…ocumentation [copilot]
…ism for SSR [copilot]
…lot] Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…logging [copilot]
…/simorgh into andrew-scss-modules_fix-amp-lite-css
… across environments and update documentation for clarity [copilot]
elvinasv
left a comment
There was a problem hiding this comment.
Clever solution!
Probably this was discussed already, but given the complexity of handling Lite/AMP dev server/HMR and the associated maintenance, could we skip dev server support for AMP/Lite and just use a production build (i.e. yarn build:local)? Would simplify this quite a bit.
| dynamicIds: Array<string | number>; | ||
| }): string => { | ||
| const isProd = process.env.NODE_ENV === 'production'; | ||
| const devCssPath = join(process.cwd(), 'build/dev-css-modules.css'); |
There was a problem hiding this comment.
Minor: potentially we could read the file name from a shared constant?
Funnily enough, this was my initial conclusion, I've obviously done a big experiment with AI in this PR as I couldn't have done the dev server in a reasonable time without it. Now although it seems to work, do you think it's worth pulling out now due to the long term maintenance? |
elvinasv
left a comment
There was a problem hiding this comment.
Personally I’m fine with not using HMR for AMP/Lite, but it might be worth consulting engineers who work with more the Lite site.
Resolves JIRA: N/A
Summary
This PR addresses @HarveyPeachey 's observation around how our other rendering strategies work with the new css modules approach:
The approach is explained in the docs accompanying this change: ws-nextjs-app/docs/amp-lite-css-extraction.md
Also with regards to @amoore108 's suggestion:
CSS with JS disabled does work with JS disabled for the production build on canonical but not for the dev build; this is a limitation of next's dev server implementation that I didn't feel it was necessary to workaround.
Useful Links