This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
feat(template): support for arbitrary replacements #1037
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b011f68
feat(template): support for arbitrary replacements
a87102f
feat(template): allow multiple substitutions
b745513
Merge branch 'master' of https://github.com/sveltejs/sapper into temp…
729c885
Merge branch 'master' of https://github.com/sveltejs/sapper into temp…
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -320,12 +320,14 @@ export function get_page_handler( | |
// users can set a CSP nonce using res.locals.nonce | ||
const nonce_attr = (res.locals && res.locals.nonce) ? ` nonce="${res.locals.nonce}"` : ''; | ||
|
||
const body = template() | ||
.replace('%sapper.base%', () => `<base href="${req.baseUrl}/">`) | ||
.replace('%sapper.scripts%', () => `<script${nonce_attr}>${script}</script>`) | ||
.replace('%sapper.html%', () => html) | ||
.replace('%sapper.head%', () => `<noscript id='sapper-head-start'></noscript>${head}<noscript id='sapper-head-end'></noscript>`) | ||
.replace('%sapper.styles%', () => styles); | ||
const body = replace(template(), { | ||
...res.replacers, | ||
base: () => `<base href="${req.baseUrl}/">`, | ||
scripts: () => `<script${nonce_attr}>${script}</script>`, | ||
html: () => html, | ||
head: () => `<noscript id='sapper-head-start'></noscript>${head}<noscript id='sapper-head-end'></noscript>`, | ||
styles: () => styles | ||
}); | ||
|
||
res.statusCode = status; | ||
res.end(body); | ||
|
@@ -356,6 +358,18 @@ export function get_page_handler( | |
}; | ||
} | ||
|
||
function replace( | ||
template: string, | ||
replacers: Record<string, string | CallableFunction> | ||
) { | ||
for (const key in replacers) { | ||
if (replacers.hasOwnProperty(key)) { | ||
template = template.replace(new RegExp(`%sapper.${key}%`, 'g'), replacers[key] as any); | ||
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. Have replacers less coupled to the 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. |
||
} | ||
} | ||
return template; | ||
} | ||
|
||
function read_template(dir = build_dir) { | ||
return fs.readFileSync(`${dir}/template.html`, 'utf-8'); | ||
} | ||
|
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.
I suggest moving this below defaults, such that you can override them. Overriding defaults could be a flexible way to solve #1034, #904, #657 and similar issues. For example: including polyfills for certain user-agents, excluding scripts on others, using absolute urls in
<base>
, etc.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.
Same here as my other comment, actually.