-
-
Notifications
You must be signed in to change notification settings - Fork 428
Conversation
Forgot to run the tests... |
A "replacer" is a reducer which is called with the page content and returns the page content. A replacer can: - alter the base href for certain user-agents - replace text in the body template - omitting script when a header is set - etc. For example, omitting javascript for Internet Explorer: ```js // handler before sapper middleware (req, res, next) => { if (req.get('user-agent').toLowerCase().includes("msie")) { res.replacers = res.replacers || [] res.replacers.push((ctx) => (ctx.script = "", ctx)) } next() } ``` or inserting a footer notice on unsupported browsers: ```js (req, res, next) => { // matchesUA: https://github.com/browserslist/browserslist-useragent if (!matchesUA(req.get('user-agent'))) { res.replacers = res.replacers || [] res.replacers.push((ctx) => (ctx.body.replace(/<\/body>/, `${unsupportedBrowserNotice}</body>`), ctx)) } next() } ``` Replacers are run in the order they are defined. System replacers will run last in order: → base href → head → styles → html → script.
45f15f7
to
f2cb02c
Compare
Remove `PageContent` and simply do replacements on the template body only. I find it hard to find examples of reducers that alters script, style, base href, etc, that cannot be done by replacing `%sapper.key%` first.
@arve0 would the approach @antony mentioned of using |
I believe that does not work when running in dev- or live-mode, right? |
yeah, you're right. he said he did it in a script after build |
Man. There are at least 3 PRs open that try to accomplish this problem in different ways. I wish I had realized that before I started on mine! FWIW, you can have a look at #1642 and see what the pros and cons between the two different approaches might be. 😅 |
Thanks for putting together this PR! Sapper never had a good method of handling configuration, which made it difficult to know how to integrate this feature. However, that's been addressed in SvelteKit by adding a |
This PR gives the user control after page render, but before
res.end
, so that the HTML output can be altered. This can be handy to:Multiple issues falls into this category, "let user control HTML output", like #657, #984, #1034, #1036, #1066, etc.
Today this is possible to some degree, by monkey-patching
res.end(body)
, which is the last call inhandle_page
:Though, that may get clunky depending on your middleware call order.
This proposal spins of @vilarfg's #1037, but also lets the user:
%sapper.key%
)%sapper.key%
first)Implementation
Before responding with the page, replace text in the page body with "replacers". A "replacer" is a reducer (
TemplateReducer
) which is called with the template body and returns the body back.Replacers are run in the order they are defined and system replacers will
run last: base href → head → styles → html → script.
Examples
Omit javascript for Internet Explorer:
Insert footer notice when unsupported browser: