Skip to content

Commit

Permalink
Query params fingerprint to break cache (#315).
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed Apr 26, 2024
1 parent e4bc454 commit 9e0f287
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 10 deletions.
4 changes: 4 additions & 0 deletions cmd/build/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type content struct {
type env struct {
local string
baseurl string
fingerprint string
entrypointHTML string
entrypointJS string
cms cms
Expand All @@ -85,6 +86,7 @@ func DataSource(buildPath string, spaPath string, siteConfig readers.SiteConfig)
env := env{
local: strconv.FormatBool(Local),
baseurl: siteConfig.BaseURL,
fingerprint: siteConfig.Fingerprint,
entrypointHTML: siteConfig.EntryPointHTML,
entrypointJS: siteConfig.EntryPointJS,
cms: cms{
Expand All @@ -98,6 +100,7 @@ func DataSource(buildPath string, spaPath string, siteConfig readers.SiteConfig)
// Create env magic prop.
envStr := "export let env = { local: " + env.local +
", baseurl: '" + env.baseurl +
"', fingerprint: '" + env.fingerprint +
"', entrypointHTML: '" + env.entrypointHTML +
"', entrypointJS: '" + env.entrypointJS +
"', cms: { repo: '" + env.cms.repo +
Expand Down Expand Up @@ -453,6 +456,7 @@ func createProps(currentContent content, allContentStr string, env env) error {
", shadowContent: {}"+
", env: {local: "+env.local+
", baseurl: '"+env.baseurl+
"', fingerprint: '"+env.fingerprint+
"', entrypointJS: '"+env.entrypointJS+
"', cms: { repo: '"+env.cms.repo+
"', redirectUrl: '"+env.cms.redirectUrl+
Expand Down
5 changes: 3 additions & 2 deletions cmd/serve/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ func (w *watcher) watch(buildPath string, Build buildFunc) {
case <-timer.C:
fmt.Println("Change detected, rebuilding site")
// TODO: cancel build if new change is made before finishing
err := Build()
if err == nil && build.Doreload {
Build()
// Check for live-reload flag
if build.Doreload {
reloadC <- struct{}{}
}

Expand Down
4 changes: 2 additions & 2 deletions defaults/starters/bare/layouts/global/head.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

<title>{ title }</title>

<script type="module" src={`/${env.entrypointJS}/core/main.js`}></script>
<script type="module" src="/spa/core/main.js?{env.fingerprint}"></script>

<link rel="icon" type="image/svg+xml" href="/media/logo.svg">
<link rel='stylesheet' href={`/${env.entrypointJS}/bundle.css`}>
<link rel='stylesheet' href="/spa/bundle.css?{env.fingerprint}">
</head>
1 change: 0 additions & 1 deletion defaults/starters/bare/plenti.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"build": "public",
"entrypoint_js": ":fingerprint",
"local": {
"port": 3000
}
Expand Down
4 changes: 2 additions & 2 deletions defaults/starters/learner/layouts/global/head.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
<title>{ title }</title>

<base href="{env.baseurl}">
<script type="module" src={env.entrypointJS + "/core/main.js"}></script>
<script type="module" src={env.entrypointJS + "/core/main.js?" + env.fingerprint}></script>

<link href="https://fonts.googleapis.com/css2?family=Rubik:ital,wght@0,300;0,700;1,300&display=swap" rel="stylesheet">
<link rel="icon" type="image/svg+xml" href="logo.svg">
<link rel='stylesheet' href='global.css'>
<link rel='stylesheet' href={env.entrypointJS + '/bundle.css'}>
<link rel='stylesheet' href={env.entrypointJS + '/bundle.css?' + env.fingerprint}>
</head>
2 changes: 1 addition & 1 deletion defaults/starters/learner/plenti.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"baseurl": "/",
"build": "public",
"entrypoint_html": "global/html.svelte",
"entrypoint_js": ":fingerprint",
"entrypoint_js": "custom-spa-folder",
"local": {
"port": 3000
},
Expand Down
7 changes: 5 additions & 2 deletions readers/site_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

// SiteConfig is the site's configuration file values.
type SiteConfig struct {
Fingerprint string
BuildDir string `json:"build"`
BaseURL string `json:"baseurl"`
Theme string `json:"theme"`
Expand Down Expand Up @@ -87,11 +88,13 @@ func GetSiteConfig(basePath string) (SiteConfig, string) {
siteConfig.EntryPointHTML = "global/html.svelte"
}

// Generate a new random string
siteConfig.Fingerprint = createRandomString()

if siteConfig.EntryPointJS == "" {
siteConfig.EntryPointJS = "spa"
} else if siteConfig.EntryPointJS == ":fingerprint" {
// Generate a new random string
siteConfig.EntryPointJS = createRandomString()
siteConfig.EntryPointJS = siteConfig.Fingerprint
}

return siteConfig, configPath
Expand Down

0 comments on commit 9e0f287

Please sign in to comment.