nuxt.js - 如何使用外部 css 在 nuxtJS 中创建 amp 页面
问题描述
在我的项目上尝试了@nuxtjs/amp,主要问题是:
在“nuxt.config.js”文件中的“extractCss”是真的,它适用于所有布局&我不能为我的 ampLayout 做任何例外
为了制作一个有效的 amp 页面,我需要将所有 CSS 代码放入内部
无论如何要保留 {"extractCss": true} 并为 amp 页面例外?
解决方案
这个对我有用。尝试:
nuxt.config.js
{
hooks: {
// This hook is called before generatic static html files for SPA mode
'generate:page': ({ page }) => {
if (/^\/amp\//gi.test(page.path)) {
page.html = modifyHtml(page.html)
}
},
// This hook is called before rendering the html to the browser
'render:route': (url, page) => {
if (/^\/amp\//gi.test(url)) {
page.html = modifyHtml(page.html)
}
}
}
}
修改HTML
const modifyHtml = (html) => {
const styleConcat = renderSync({ file: 'assets/css/amp.scss', outputStyle: 'compressed' }).css.toString()
html = html.replace('</head>', `<style amp-custom>${styleConcat}</style></head>`)
html = html.replace(/<link[^>]*rel="stylesheet" href=".*\.css[^>]*>/gi, '')
html = html.replace(/<link[^>]*rel="preload" href=".*\.js[^>]*>/gi, '')
return html
}
推荐阅读
- git - Git 推送到 Github 后会删除本地文件吗?
- java - Java日期返回1970?
- python - 按字符串属性对对象列表进行排序
- node.js - Firebase 函数获取键名列表
- c# - 如何在 C# 中使用 ConfigurationManager 保存到漫游配置文件
- python - 如何防止 gdal.ReprojectImage 忽略零?
- c# - 试图弄清楚如何在我的代码中传递“字面值 20”
- javascript - 使用 React js Modal 的动态内容
- excel - excel表格中的x/y值列表
- angular - 在 Storybook 中使用 @angular-redux/store 测试 Angular