-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
69 additions
and
98 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,26 +1,29 @@ | ||
import {Response, Status, MediaType} from "runtime-compat/http"; | ||
|
||
const script = /(?<=<script)>(?<code>.*?)(?=<\/script>)/gus; | ||
const style = /(?<=<style)>(?<code>.*?)(?=<\/style>)/gus; | ||
const script_re = /(?<=<script)>(?<code>.*?)(?=<\/script>)/gus; | ||
const style_re = /(?<=<style)>(?<code>.*?)(?=<\/style>)/gus; | ||
const remove = /<(?<tag>script|style)>.*?<\/\k<tag>>/gus; | ||
const inline = true; | ||
|
||
export default (name, options = {}) => { | ||
const {status = Status.OK, partial = false} = options; | ||
|
||
return async app => { | ||
const html = await app.path.components.join(name).text(); | ||
await Promise.all([...html.matchAll(script)] | ||
.map(({groups: {code}}) => app.publish({code, inline}))); | ||
await Promise.all([...html.matchAll(style)] | ||
.map(({groups: {code}}) => app.publish({code, type: "style", inline}))); | ||
const scripts = await Promise.all([...html.matchAll(script_re)] | ||
.map(({groups: {code}}) => app.inline(code, "module"))); | ||
const styles = await Promise.all([...html.matchAll(style_re)] | ||
.map(({groups: {code}}) => app.inline(code, "style"))); | ||
const assets = [...scripts, ...styles]; | ||
|
||
const body = html.replaceAll(remove, _ => ""); | ||
// needs to happen before app.render() | ||
const headers = app.headers(); | ||
const head = assets.map(asset => asset.head).join("\n"); | ||
const script = scripts.map(asset => asset.csp).join(" "); | ||
const style = styles.map(asset => asset.csp).join(" "); | ||
const headers = {script, style}; | ||
|
||
return new Response(partial ? body : await app.render({body}), { | ||
return new Response(partial ? body : await app.render({body, head}), { | ||
status, | ||
headers: {...headers, "Content-Type": MediaType.TEXT_HTML}, | ||
headers: {...app.headers(headers), "Content-Type": MediaType.TEXT_HTML}, | ||
}); | ||
}; | ||
}; |
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