Skip to content

Commit 1371450

Browse files
committed
fix: bypass Cloudflare Rocket Loader with data-cfasync=false on module script
1 parent 03c8b2f commit 1371450

4 files changed

Lines changed: 28 additions & 2 deletions

File tree

_headers

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
Referrer-Policy: strict-origin-when-cross-origin
55
Permissions-Policy: camera=(), microphone=(), geolocation=()
66
Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; connect-src 'self' https:; frame-ancestors 'none'
7+
Cache-Control: no-cache, no-store, must-revalidate
8+
Pragma: no-cache
9+
Expires: 0
10+
11+
/assets/*
12+
Cache-Control: public, max-age=31536000, immutable
713

814
/.well-known/*
915
Access-Control-Allow-Origin: *

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
2929
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800;900&family=JetBrains+Mono:wght@400;500;600;700&display=swap" rel="stylesheet" />
3030
<title>QECTOR · Production-Grade Quantum Error Correction Decoding for Python</title>
31-
<script type="module" crossorigin src="/assets/index-CaGUdrOG.js"></script>
31+
<script type="module" data-cfasync="false" crossorigin src="/assets/index-CaGUdrOG.js"></script>
3232
<link rel="stylesheet" crossorigin href="/assets/index-B4eLQ2k1.css">
3333
</head>
3434
<body>

source/public/_headers

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
Referrer-Policy: strict-origin-when-cross-origin
55
Permissions-Policy: camera=(), microphone=(), geolocation=()
66
Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; connect-src 'self' https:; frame-ancestors 'none'
7+
Cache-Control: no-cache, no-store, must-revalidate
8+
Pragma: no-cache
9+
Expires: 0
10+
11+
/assets/*
12+
Cache-Control: public, max-age=31536000, immutable
713

814
/.well-known/*
915
Access-Control-Allow-Origin: *

source/vite.config.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,27 @@ import { defineConfig } from "vite"
44
import { inspectAttr } from 'kimi-plugin-inspect-react'
55

66
// https://vite.dev/config/
7+
// Cloudflare Rocket Loader rewrites <script type="module"> to
8+
// <script type="<random>-module">, breaking ESM and causing the browser to
9+
// download files instead. The data-cfasync="false" attribute tells Cloudflare
10+
// to skip Rocket Loader for that tag.
11+
function cfRocketBypass(): import('vite').Plugin {
12+
return {
13+
name: 'cf-rocket-bypass',
14+
enforce: 'post',
15+
transformIndexHtml(html) {
16+
return html.replace(/(<script\s+type="module")/g, '$1 data-cfasync="false"')
17+
},
18+
}
19+
}
20+
721
export default defineConfig(({ command }) => ({
822
base: '/',
923
// inspectAttr() tags every JSX element with source file/line data attributes.
1024
// Useful in the editor while developing, but it has no business shipping to
1125
// production: it bloats the bundle and leaks internal file paths into the
1226
// live HTML. Only include it when Vite is running the dev server.
13-
plugins: [...(command === 'serve' ? [inspectAttr()] : []), react()],
27+
plugins: [...(command === 'serve' ? [inspectAttr()] : []), cfRocketBypass(), react()],
1428
server: {
1529
port: 3000,
1630
},

0 commit comments

Comments
 (0)