fix: try to solve the issue that css file not found in ssr mode #12
+67
−12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
背景:
我们之前的方案是在 SSR 和 SSG 时根据当前访问页面使用到的组件按需抽取样式并输出到导出目录,从而可以利用浏览器的缓存机制更好的加载组件样式。
这种方式在 SSG 模式看上去是没有问题的,因为 SSG 模式会在运行 build 命令时就会生成样式文件,当我们运行 start 命令时,这些文件就会被加入到 nextjs 的静态文件服务中管理。
但是,SSR 模式 并不会在 build 阶段抽取生成样式,而是在用户第一次访问页面时才会抽取生成,此时 nextjs 的静态文件服务并不会将新生成的样式文件自动纳入管理,因此导致了如下问题:#11。
为了解决这个问题,这边尝试利用 nextjs 的 api ,实现一个接口,通过传入目标样式名称给 loadCss 接口,让接口查找目标样式文件并输出。
为了尽可能利用浏览器的缓存机制,我们需要在 loadCss 接口当中设置一定的缓存策略,让浏览器接受到接口响应后按要求缓存内容。
此方案目前处于实验阶段,具体是否可行还需讨论。