Jelajahi Sumber

feat(ssr): auto-remove initial state script if prod (#6763)

close #6761
Mark Brouch 8 tahun lalu
induk
melakukan
2d32b5d1b6
1 mengubah file dengan 4 tambahan dan 1 penghapusan
  1. 4 1
      src/server/template-renderer/index.js

+ 4 - 1
src/server/template-renderer/index.js

@@ -191,10 +191,13 @@ export default class TemplateRenderer {
       contextKey = 'state',
       windowKey = '__INITIAL_STATE__'
     } = options || {}
+    const autoRemove = process.env.NODE_ENV === 'production'
+      ? '(function(){var s;(s=document.currentScript||document.scripts[document.scripts.length-1]).parentNode.removeChild(s);}());'
+      : ''
     return context[contextKey]
       ? `<script>window.${windowKey}=${
           serialize(context[contextKey], { isJSON: true })
-        }</script>`
+        }${autoRemove}</script>`
       : ''
   }