Last active
September 28, 2024 21:04
-
-
Save ccollie/5e86724a11611560572529774b7faead to your computer and use it in GitHub Desktop.
Evidence: Work around for reports not rooted at "/"
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
<script> | |
import '@evidence-dev/tailwind/fonts.css'; | |
import '../app.css'; | |
import { EvidenceDefaultLayout } from '@evidence-dev/core-components'; | |
export let data; | |
</script> | |
<svelte:head> | |
<script> | |
// CHANGE ME | |
const __REPORT_ROOT__ = `/report-viewer`; | |
function normalizeUrl(url) { | |
if (url && url.startsWith('/') && !url.startsWith(__REPORT_ROOT__)) { | |
url = __REPORT_ROOT__ + url; | |
} | |
return url; | |
} | |
function processManifest(manifest) { | |
const { renderedFiles } = manifest; | |
if (!renderedFiles) return manifest; | |
for (const schema in renderedFiles) { | |
const files = renderedFiles[schema]; | |
files.forEach((url, index) => { | |
if (url.startsWith('static')) { | |
url = url.substring(6); | |
} | |
files[index] = normalizeUrl(url); | |
}); | |
} | |
return manifest; | |
} | |
// Save the original fetch function | |
const originalFetch = window.fetch; | |
function fetchProxy(...args) { | |
let url = args[0]; | |
const isManifest = url.endsWith('data/manifest.json') && !url.startsWith('http'); | |
args[0] = normalizeUrl(url); | |
const res = originalFetch.apply(null, args); | |
if (isManifest) { | |
return res | |
.then(response => response.json()) | |
.then(processManifest) | |
.then(data => { | |
return new Response(JSON.stringify(data), { | |
status: 200, | |
headers: { | |
'Content-Type': 'application/json' | |
} | |
}); | |
}); | |
} | |
return res; | |
} | |
window.fetch = fetchProxy; | |
</script> | |
</svelte:head> | |
<EvidenceDefaultLayout {data}> | |
<slot slot="content" /> | |
</EvidenceDefaultLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note for future viewers - do not add query string parameters to the original url in
normalizeUrl
. This will cause the report to error out after the initial render.