Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/server/template-renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,13 @@ export default class TemplateRenderer {
contextKey = 'state',
windowKey = '__INITIAL_STATE__'
} = options || {}
const autoRemove = process.env.NODE_ENV === 'production'
? 'var s;(s=document.currentScript||document.scripts[document.scripts.length-1]).parentNode.removeChild(s);'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document.currentScript is not supported in IE yet, hence the fallback.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block should be wrapped in an IIFE to avoid leaking s to global scope.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 2409ff0

: ''
return context[contextKey]
? `<script>window.${windowKey}=${
serialize(context[contextKey], { isJSON: true })
}</script>`
}${autoRemove}</script>`
: ''
}

Expand Down