Skip to content

Commit 7b0ad66

Browse files
authored
fix: query parameters are preserved (#636)
* test: add query parameter test to show failure * test: make ipfs-gateway script more robust * chore: allow for running locally testable sw-gateway * fix: query parameters are preserved after redirect * test: _redirects dist confirmation update * test: add handleFirstHit test * test: ensure first-hit with extra query param passes * test: all tests passing * refactor: ensure use of getHeliaSwRedirectUrl * chore: refactoring some things... * chore: package-lock.json and minor documentation update * chore: remove unused * refactor: improve getHeliaSwRedirectUrl * refactor: normalize toSubdomainRequest * chore: fix lint and cleanup unnecessary checks
1 parent 2f24692 commit 7b0ad66

16 files changed

+604
-1236
lines changed

build.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,30 @@ const injectAssets = (metafile) => {
8585
console.log(`Injected ${path.basename(scriptFile)} and ${path.basename(cssFile)} into index.html.`)
8686
}
8787

88+
/**
89+
* Inject the ipfs-sw-first-hit.js into the ipfs-sw-first-hit.html file
90+
*
91+
* This was added when addressing an issue with redirects not preserving query parameters.
92+
*
93+
* The solution we're moving forward with is, instead of using 302 redirects with ipfs _redirects file, we are
94+
* using 200 responses with the ipfs-sw-first-hit.html file. That file will include the ipfs-sw-first-hit.js script
95+
* which will be injected into the index.html file, and handle the redirect logic for us.
96+
*
97+
* @see https://github.com/ipfs/service-worker-gateway/issues/628
98+
*
99+
* @param {esbuild.Metafile} metafile
100+
*/
101+
const injectFirstHitJs = (metafile) => {
102+
const htmlFilePath = path.resolve('dist/ipfs-sw-first-hit.html')
103+
104+
const scriptFile = Object.keys(metafile.outputs).find(file => file.endsWith('.js') && file.includes('ipfs-sw-first-hit'))
105+
const scriptTag = `<script src="/${path.basename(scriptFile)}"></script>`
106+
let htmlContent = fs.readFileSync(htmlFilePath, 'utf8')
107+
htmlContent = htmlContent.replace(/<%= GIT_VERSION %>/g, gitRevision())
108+
htmlContent = htmlContent.replace('</body>', `${scriptTag}</body>`)
109+
fs.writeFileSync(htmlFilePath, htmlContent)
110+
}
111+
88112
/**
89113
* We need the service worker to have a consistent name
90114
*
@@ -126,6 +150,7 @@ const modifyBuiltFiles = {
126150
build.onEnd(async (result) => {
127151
copyPublicFiles()
128152
injectAssets(result.metafile)
153+
injectFirstHitJs(result.metafile)
129154
})
130155
}
131156
}
@@ -149,7 +174,7 @@ const excludeFilesPlugin = (extensions) => ({
149174
* @type {esbuild.BuildOptions}
150175
*/
151176
export const buildOptions = {
152-
entryPoints: ['src/index.tsx', 'src/sw.ts'],
177+
entryPoints: ['src/index.tsx', 'src/sw.ts', 'src/ipfs-sw-first-hit.ts'],
153178
bundle: true,
154179
outdir: 'dist',
155180
loader: {

0 commit comments

Comments
 (0)