Skip to content

Commit

Permalink
fix: html rewrite cache should be based on content
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 20, 2020
1 parent 25852fa commit 3752874
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions src/node/server/serverPluginHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ const debug = require('debug')('vite:rewrite')

const rewriteHtmlPluginCache = new LRUCache({ max: 20 })

export const htmlPlugin: ServerPlugin = ({
root,
app,
watcher,
resolver,
config
}) => {
export const htmlPlugin: ServerPlugin = ({ root, app, watcher, resolver }) => {
// inject __DEV__ and process.env.NODE_ENV flags
// since some ESM builds expect these to be replaced by the bundler
const devInjectionCode =
Expand Down Expand Up @@ -71,17 +65,16 @@ export const htmlPlugin: ServerPlugin = ({
return
}

const { path } = ctx

if (ctx.response.is('html')) {
if (rewriteHtmlPluginCache.has(path)) {
debug(`${path}: serving from cache`)
ctx.body = rewriteHtmlPluginCache.get(path)
if (ctx.response.is('html') && ctx.body) {
const importer = ctx.path
const html = await readBody(ctx.body)
if (rewriteHtmlPluginCache.has(html)) {
debug(`${ctx.path}: serving from cache`)
ctx.body = rewriteHtmlPluginCache.get(html)
} else {
const html = await readBody(ctx.body)
if (!html) return
ctx.body = await rewriteHtml(path, html)
rewriteHtmlPluginCache.set(path, ctx.body)
ctx.body = await rewriteHtml(importer, html)
rewriteHtmlPluginCache.set(html, ctx.body)
}
return
}
Expand All @@ -90,7 +83,6 @@ export const htmlPlugin: ServerPlugin = ({
watcher.on('change', (file) => {
const path = resolver.fileToRequest(file)
if (path.endsWith('.html')) {
rewriteHtmlPluginCache.del(path)
debug(`${path}: cache busted`)
watcher.send({
type: 'full-reload',
Expand Down

0 comments on commit 3752874

Please sign in to comment.