-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
warn if comments are removed from HTML #8423
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bcad8de
warn if comments are removed from HTML - closes #7493
Rich-Harris 3de6e3a
use esm-env
Rich-Harris d5659b9
Update packages/kit/src/runtime/server/page/render.js
Rich-Harris a4d2439
add explanatory comment
Rich-Harris 73ce487
Merge branch 'gh-7493' of github.com:sveltejs/kit into gh-7493
Rich-Harris 90dc980
prettier
Rich-Harris cc4644a
only warn if csr is true
dummdidumm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@sveltejs/kit': patch | ||
| --- | ||
|
|
||
| Warn if comments are removed from HTML |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import { s } from '../../../utils/misc.js'; | |
| import { Csp } from './csp.js'; | ||
| import { uneval_action_response } from './actions.js'; | ||
| import { clarify_devalue_error } from '../utils.js'; | ||
| import { DEV } from 'esm-env'; | ||
|
|
||
| // TODO rename this function/module | ||
|
|
||
|
|
@@ -353,17 +354,34 @@ export async function render_response({ | |
| // add the content after the script/css links so the link elements are parsed first | ||
| head += rendered.head; | ||
|
|
||
| const html = options.app_template({ | ||
| head, | ||
| body, | ||
| assets, | ||
| nonce: /** @type {string} */ (csp.nonce) | ||
| }); | ||
|
|
||
| // TODO flush chunks as early as we can | ||
| const html = | ||
| const transformed = | ||
| (await resolve_opts.transformPageChunk({ | ||
| html: options.app_template({ head, body, assets, nonce: /** @type {string} */ (csp.nonce) }), | ||
| html, | ||
| done: true | ||
| })) || ''; | ||
|
|
||
| if (DEV && page_config.csr) { | ||
| if (transformed.split('<!--').length < html.split('<!--').length) { | ||
| // the \u001B stuff is ANSI codes, so that we don't need to add a library to the runtime | ||
| // https://svelte.dev/repl/1b3f49696f0c44c881c34587f2537aa2 | ||
| console.warn( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should emit this warning only if |
||
| "\u001B[1m\u001B[31mRemoving comments in transformPageChunk can break Svelte's hydration\u001B[39m\u001B[22m" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| const headers = new Headers({ | ||
| 'x-sveltekit-page': 'true', | ||
| 'content-type': 'text/html', | ||
| etag: `"${hash(html)}"` | ||
| etag: `"${hash(transformed)}"` | ||
| }); | ||
|
|
||
| if (!state.prerendering) { | ||
|
|
@@ -381,7 +399,7 @@ export async function render_response({ | |
| } | ||
| } | ||
|
|
||
| return new Response(html, { | ||
| return new Response(transformed, { | ||
| status, | ||
| headers | ||
| }); | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
something like
transformed.match(/<!--/g).lengthmight be less expensive as i think it wouldn't create large arrays (i'm not a regex expert - that might need some escaping or something)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String.prototype.matchcalled with a global regex does return a regular array anyway (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match), so unless there's some clever optimization that works in one case but not the other, we're not really avoiding anything by doing that.I don't think we want to worry overly much about performance here anyway, because it is a dev-only check,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
huh TIL about
match. What a fantastically useless API! Struggling to imagine a single use case for it other than ours — counting — for which allocating an array is incredibly wasteful. What the hell am I supposed to do with the returned strings, without indexes etc?It's
.match(string)that really kills me though — it will return an array but it will only ever have a length of 1 and its value will only ever be[input]:Anyway, this was a good hypothesis but I just ran some microbenchmarks...
...and the
matchversion is a full two orders of magnitude slower (7.4s vs 69ms — nice — on my machine).