-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: getHashedStaticPath
, append hashes to URLs for cache busting
#431
Closed
Closed
Changes from all commits
Commits
Show all changes
110 commits
Select commit
Hold shift + click to select a range
c55e28a
hash initial
gurgunday 491859e
refactor
gurgunday 2ab65a0
use glob
gurgunday 65ed7c6
hashes
gurgunday d5a67e2
merge main
gurgunday 7bdd289
change msg
gurgunday 1dbf421
remove dynamic unhash
gurgunday c910b78
fix
gurgunday 26ec2ba
fix
gurgunday f09d85a
fix
gurgunday 4a3a40e
add tests
gurgunday 5e21320
add tests
gurgunday 9fb0a2a
remove comment
gurgunday ab1c1ff
fix windows
gurgunday 72f8965
escape more consistently
gurgunday 731a3f3
fix error
gurgunday f70bbe3
format
gurgunday a00c931
add type
gurgunday ce1a788
add README
gurgunday 1ea8ae8
typo
gurgunday 67c3add
use globSync
gurgunday 07250db
tests
gurgunday 403e7f8
tests
gurgunday 3276917
windows
gurgunday be525c2
posix
gurgunday 64b1073
don't call path.join
gurgunday f75aed3
use foo
gurgunday 587c5c4
simplify
gurgunday 20b5a26
example
gurgunday ca3606a
return pathname if hash gen fails
gurgunday 0194e69
use glob
gurgunday 9dff32b
refactor
gurgunday 3bd86ff
really ignore files
gurgunday 5075b62
type
gurgunday 76968b4
make sure getHashedAsset returns a string
gurgunday e607765
add example
gurgunday 1feca50
Add fastq dependency for task queueing
gurgunday 2f84451
typo
gurgunday ff342d6
replace p-limit with fastq
gurgunday 88989b7
add back jsdoc
gurgunday c231e52
remove copilot comments
gurgunday 8594fb7
top lvl await
gurgunday 5fb4046
use availableParallelism
gurgunday 16d436b
typo
gurgunday fce66eb
pollyfill
gurgunday 6ad51f0
ignore
gurgunday 03b9f78
use async iterator
gurgunday bc9c061
use async iterator
gurgunday 0fb66bb
start with undefined
gurgunday c5cf0ba
use Glob
gurgunday f92aba2
hash
gurgunday d046eb0
remove istanbul
gurgunday 1b65f15
add another test
gurgunday c3a21b6
update script
gurgunday efa2900
extract tmp
gurgunday e3f7bc3
Refactor generateHashes function signature
gurgunday e28166e
make sure node_modules is not there and add another test
gurgunday f257e5c
use os.cpus
gurgunday 9efd8d5
update signature
gurgunday 4e4b2fb
bump
gurgunday 8d8cd42
update readme
gurgunday a06f496
typo
gurgunday 3a5b9e9
improve readme
gurgunday 923ab52
update readme
gurgunday 0e4f5c9
typo
gurgunday 5b8f8ff
update readme
gurgunday a794124
update test
gurgunday 9f12cac
move the script to the README
gurgunday 720629a
typo
gurgunday a11247b
more docs
gurgunday a6c7bef
use fastify.route
gurgunday a9d3534
lint
gurgunday a58b39f
remove dup test
gurgunday 49c761f
use symbol
gurgunday fea7e75
remove unused error init
gurgunday 083fb93
simplify
gurgunday 9fd9adf
Fix teardown function in static.test.js
gurgunday 41f10f0
disable wildcard by default
gurgunday 33f4930
update README
gurgunday 0ebbfbd
don't generate empty object for no reason
gurgunday a5785d1
reorder imports
gurgunday 28b6347
reorder imports
gurgunday 965b668
rename var
gurgunday 0085e5b
memory: only store hashes in the map
gurgunday 0e4fbea
make path configurable
gurgunday ae10ffe
simplify and revert name change
gurgunday c9393f5
remove unrelated changes
gurgunday 78ed80a
remove unrelated changes
gurgunday b03ff10
use hashPath instead of location
gurgunday 07b0244
refactor getHashedAsset
gurgunday 7969fbd
add pregenerated example
gurgunday a5c83ef
use node: imports
gurgunday 942bccc
use node: imports
gurgunday b504798
Merge branch 'master' into hash
gurgunday 9e3f9b3
use executable scripts
gurgunday 3ab6db6
Merge branch 'hash' of https://github.com/gurgunday/fastify-static in…
gurgunday 3c37ab4
update readme
gurgunday b4475b7
dd hash-gen
gurgunday f4274c7
change params
gurgunday e489afd
typo
gurgunday ded393d
move to bin
gurgunday 64fd79c
apply suggestions 1
gurgunday fd94f70
apply suggestions 2
gurgunday 14da551
use named param
gurgunday 8428987
append to the end
gurgunday 5b47060
call it skipPatterns
gurgunday 19f1cf6
Merge branch 'master' of https://github.com/fastify/fastify-static in…
gurgunday 418b9c8
Merge branch 'master' of https://github.com/fastify/fastify-static in…
gurgunday ccea963
only throw when not file not found error
gurgunday 83a1a77
use ?hash=
gurgunday File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env node | ||
|
||
'use strict' | ||
|
||
const { generateHashes } = require('../lib/hash') | ||
const path = require('path') | ||
|
||
async function run () { | ||
let [rootPaths, writeLocation, includeDotFiles, ignorePatterns] = process.argv.slice(2) | ||
|
||
if (rootPaths === undefined || writeLocation === undefined) { | ||
console.error('Usage: hash <root-paths> <write-location> <include-dot-files> <ignore-patterns>') | ||
process.exit(1) | ||
} | ||
|
||
rootPaths = rootPaths.split(',').map(p => path.resolve(p.trim())) | ||
includeDotFiles = includeDotFiles === 'true' | ||
ignorePatterns = ignorePatterns ? ignorePatterns.split(',') : [] | ||
|
||
try { | ||
await generateHashes({ | ||
rootPaths, | ||
includeDotFiles, | ||
skipPatterns: ignorePatterns, | ||
writeToFile: true, | ||
outputPath: writeLocation | ||
} | ||
) | ||
console.log('Hashes generated successfully.') | ||
} catch (error) { | ||
console.error('Error generating hashes:', error) | ||
} | ||
} | ||
|
||
run() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"index.js": "1f49271e60e53b00", | ||
"index.html": "8e27d2f424d541b4", | ||
"index.css": "712708f47c759d37", | ||
"images/sample.jpg": "4c67e0488b3afa81", | ||
"test.html": "6944c8b16078a588", | ||
"test.css": "b305efc33fe211f7" | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import fastify from 'fastify' | ||
import fastifyStatic from '../index.js' | ||
|
||
const server = fastify() | ||
|
||
await server.register(fastifyStatic, { | ||
root: new URL('./public', import.meta.url).pathname, | ||
prefix: '/assets/', | ||
hash: { path: new URL('hashes.json', import.meta.url).pathname }, | ||
immutable: true, | ||
maxAge: 31536000 * 1000 // 1 year | ||
}) | ||
|
||
server.register(import('fastify-html')) | ||
|
||
// Define a route for the root URL | ||
server.get('/', async (request, reply) => { | ||
return reply.html` | ||
<html> | ||
<head> | ||
<link rel="stylesheet" href="${server.getHashedStaticPath('index.css')}"> | ||
</head> | ||
<body> | ||
<h1>Hello, world!</h1> | ||
<img src="${server.getHashedStaticPath('images/sample.jpg')}" alt="An image"> | ||
</body> | ||
</html> | ||
` | ||
}) | ||
|
||
// Start the server | ||
await server.listen({ port: 3000 }) | ||
console.log('Server is running on port 3000') |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import fastify from 'fastify' | ||
import fastifyStatic from '../index.js' | ||
|
||
const server = fastify() | ||
|
||
await server.register(fastifyStatic, { | ||
root: new URL('./public', import.meta.url).pathname, | ||
prefix: '/assets/', | ||
hash: true, | ||
immutable: true, | ||
maxAge: 31536000 * 1000 // 1 year | ||
}) | ||
|
||
server.register(import('fastify-html')) | ||
|
||
// Define a route for the root URL | ||
server.get('/', async (request, reply) => { | ||
return reply.html` | ||
<html> | ||
<head> | ||
<link rel="stylesheet" href="${server.getHashedStaticPath('index.css')}"> | ||
</head> | ||
<body> | ||
<h1>Hello, world!</h1> | ||
<img src="${server.getHashedStaticPath('images/sample.jpg')}" alt="An image"> | ||
</body> | ||
</html> | ||
` | ||
}) | ||
|
||
// Start the server | ||
await server.listen({ port: 3000 }) | ||
console.log('Server is running on port 3000') |
This file contains 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to be annoying about names, this is take-it-or-leave-it feedback, but just reading this code without reading the readme, I feel like the option
hash: true
could mean a variety of different things. I'd probably guess it means ""add an
e-tag
header", not that it allows fancy path building for CDN busting hashes.