Skip to content

Commit

Permalink
Merge pull request #2053 from OpenNeuroOrg/ssr-improve-cache-performance
Browse files Browse the repository at this point in the history
Increase cache duration for SSR resources
  • Loading branch information
nellh authored Apr 14, 2021
2 parents a2b8033 + b8ec2a6 commit f570c39
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions packages/openneuro-app/src/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ async function createServer(): Promise<void> {
} else {
app.use(
'/assets',
express.static(path.resolve(__dirname, '../src/dist/client/assets')),
express.static(path.resolve(__dirname, '../src/dist/client/assets'), {
maxAge: '1y',
}),
)
}

Expand All @@ -68,13 +70,19 @@ async function createServer(): Promise<void> {
if (url === '/sw.js') {
res
.status(200)
.set({ 'Content-Type': 'application/javascript' })
.set({
'Content-Type': 'application/javascript',
'Cache-Control': 'public, max-age=0',
})
.end(selfDestroyingServiceWorker)
return
} else if (url === '/config.js') {
res
.status(200)
.set({ 'Content-Type': 'application/javascript' })
.set({
'Content-Type': 'application/javascript',
'Cache-Control': 'public, max-age=0',
})
.end(configScript)
return
}
Expand Down Expand Up @@ -117,8 +125,21 @@ async function createServer(): Promise<void> {
return replace
})

// Cache rendered pages for up to one hour
let cacheControl = 'private, max-age=3600'
if (req['universalCookies'].get('accessToken') === undefined) {
// Allow proxies to cache anonymous requests
cacheControl = 'public, max-age=3600'
}

// 6. Send the rendered HTML back.
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
res
.status(200)
.set({
'Content-Type': 'text/html',
'Cache-Control': cacheControl,
})
.end(html)
} catch (e) {
// If an error is caught, let vite fix the stacktrace so it maps back to
// your actual source code.
Expand Down

0 comments on commit f570c39

Please sign in to comment.