(response: Response)
| 97 | */ |
| 98 | export class CachedDocument extends Response { |
| 99 | static async from(response: Response) { |
| 100 | const text = await response.text() |
| 101 | |
| 102 | // Set the content-type explicitly. Some browsers (Android 12; Chrome 91) use an invalid content-type header. |
| 103 | const headers = new Headers(response.headers) |
| 104 | headers.set('Content-Type', 'text/html; charset=utf-8') |
| 105 | const init = { ...response, headers } |
| 106 | |
| 107 | // Injects a marker into the document so that client code knows it was served from cache. |
| 108 | // The marker should be injected immediately in the <body> so it is available to client code. |
| 109 | return new CachedDocument(text.replace('<body>', '<body><script>window.__isDocumentCached=true</script>'), init) |
| 110 | } |
| 111 | |
| 112 | private constructor(text: string, response: Response) { |
| 113 | super(text, response) |
no outgoing calls