Skip to content
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

Remove cache busting #23

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions gulp/buildutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,4 @@ export function getTag() {
export function getVersion() {
// Use the version number specified in package.json
return JSON.parse(fs.readFileSync("../package.json", "utf-8")).version;
}

/**
* @param {string} url
* @param {string} commitHash
*/
export function cachebust(url, commitHash) {
return "/v/" + commitHash + "/" + url;
}
}
52 changes: 16 additions & 36 deletions gulp/css.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "path/posix";
import { getRevision, cachebust } from "./buildutils.js";
import { getRevision } from "./buildutils.js";

import gulpPostcss from "gulp-postcss";
import postcssAssets from "postcss-assets";
Expand All @@ -16,21 +16,15 @@ import gulpRename from "gulp-rename";
export default function gulptasksCSS(gulp, buildFolder, browserSync) {
// The assets plugin copies the files
const commitHash = getRevision();
const postcssAssetsPlugin = enableCachebust =>
postcssAssets({
loadPaths: [path.join(buildFolder, "res", "ui")],
basePath: buildFolder,
baseUrl: ".",
cachebuster: enableCachebust
? (filePath, urlPathname) => ({
pathname: cachebust(urlPathname, commitHash),
})
: "",
});
const postcssAssetsPlugin = postcssAssets({
loadPaths: [path.join(buildFolder, "res", "ui")],
basePath: buildFolder,
baseUrl: ".",
});

// Postcss configuration
const postcssPlugins = (prod, { cachebust = false }) => {
const plugins = [postcssAssetsPlugin(cachebust)];
const postcssPlugins = prod => {
const plugins = [postcssAssetsPlugin];
if (prod) {
plugins.unshift(
postcssPresetEnv({
Expand Down Expand Up @@ -69,7 +63,7 @@ export default function gulptasksCSS(gulp, buildFolder, browserSync) {
.pipe(gulpSassLint.failOnError());
});

function resourcesTask({ cachebust, isProd }) {
function resourcesTask({ isProd }) {
return gulp
.src("../src/css/main.scss")
.pipe(gulpPlumber())
Expand All @@ -82,27 +76,22 @@ export default function gulptasksCSS(gulp, buildFolder, browserSync) {
])
)
.pipe(gulpRename("async-resources.css"))
.pipe(gulpPostcss(postcssPlugins(isProd, { cachebust })))
.pipe(gulpPostcss(postcssPlugins(isProd)))
.pipe(gulp.dest(buildFolder))
.pipe(browserSync.stream());
}

// Builds the css resources
gulp.task("css.resources.dev", () => {
return resourcesTask({ cachebust: false, isProd: false });
return resourcesTask({ isProd: false });
});

// Builds the css resources in prod (=minified)
gulp.task("css.resources.prod", () => {
return resourcesTask({ cachebust: true, isProd: true });
});

// Builds the css resources in prod (=minified), without cachebusting
gulp.task("css.resources.prod-standalone", () => {
return resourcesTask({ cachebust: false, isProd: true });
return resourcesTask({ isProd: true });
});

function mainTask({ cachebust, isProd }) {
function mainTask({ isProd }) {
return gulp
.src("../src/css/main.scss")
.pipe(gulpPlumber())
Expand All @@ -115,30 +104,21 @@ export default function gulptasksCSS(gulp, buildFolder, browserSync) {
}),
])
)
.pipe(gulpPostcss(postcssPlugins(isProd, { cachebust })))
.pipe(gulpPostcss(postcssPlugins(isProd)))
.pipe(gulp.dest(buildFolder))
.pipe(browserSync.stream());
}

// Builds the css main
gulp.task("css.main.dev", () => {
return mainTask({ cachebust: false, isProd: false });
return mainTask({ isProd: false });
});

// Builds the css main in prod (=minified)
gulp.task("css.main.prod", () => {
return mainTask({ cachebust: true, isProd: true });
});

// Builds the css main in prod (=minified), without cachebusting
gulp.task("css.main.prod-standalone", () => {
return mainTask({ cachebust: false, isProd: true });
return mainTask({ isProd: true });
});

gulp.task("css.dev", gulp.parallel("css.main.dev", "css.resources.dev"));
gulp.task("css.prod", gulp.parallel("css.main.prod", "css.resources.prod"));
gulp.task(
"css.prod-standalone",
gulp.parallel("css.main.prod-standalone", "css.resources.prod-standalone")
);
}
5 changes: 1 addition & 4 deletions gulp/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,7 @@ for (const variant in BUILD_VARIANTS) {

gulp.task(buildName + ".resourcesAndCode", gulp.parallel("step.baseResources", buildName + ".code"));

gulp.task(
buildName + ".all",
gulp.series(buildName + ".resourcesAndCode", "css.prod-standalone", "html.prod")
);
gulp.task(buildName + ".all", gulp.series(buildName + ".resourcesAndCode", "css.prod", "html.prod"));

gulp.task(buildName, gulp.series("utils.cleanup", buildName + ".all", "step.postbuild"));

Expand Down
6 changes: 2 additions & 4 deletions src/js/core/background_resources_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { initSpriteCache } from "../game/meta_building_registry";
import { MUSIC, SOUNDS } from "../platform/sound";
import { T } from "../translations";
import { AtlasDefinition, atlasFiles } from "./atlas_definitions";
import { cachebust } from "./cachebust";
import { Loader } from "./loader";
import { createLogger } from "./logging";
import { Signal } from "./signal";
Expand Down Expand Up @@ -200,8 +199,7 @@ export class BackgroundResourcesLoader {
const xhr = new XMLHttpRequest();
let notifiedNotComputable = false;

const fullUrl = cachebust(src);
xhr.open("GET", fullUrl, true);
xhr.open("GET", src, true);
xhr.responseType = "arraybuffer";
xhr.onprogress = function (ev) {
if (ev.lengthComputable) {
Expand All @@ -225,7 +223,7 @@ export class BackgroundResourcesLoader {

xhr.onloadend = function () {
if (!xhr.status.toString().match(/^2/)) {
reject(fullUrl + ": " + xhr.status + " " + xhr.statusText);
reject(src + ": " + xhr.status + " " + xhr.statusText);
} else {
if (!notifiedNotComputable) {
progressHandler(1);
Expand Down
10 changes: 0 additions & 10 deletions src/js/core/cachebust.js

This file was deleted.

1 change: 0 additions & 1 deletion src/js/core/loader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { makeOffscreenBuffer } from "./buffer_utils";
import { AtlasSprite, BaseSprite, RegularSprite, SpriteAtlasLink } from "./sprites";
import { cachebust } from "./cachebust";
import { createLogger } from "./logging";

/**
Expand Down
3 changes: 1 addition & 2 deletions src/js/game/hud/parts/interactive_tutorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { GameRoot } from "../../root";
import { MinerComponent } from "../../components/miner";
import { DynamicDomAttach } from "../dynamic_dom_attach";
import { TrackedState } from "../../../core/tracked_state";
import { cachebust } from "../../../core/cachebust";
import { T } from "../../../translations";
import { enumItemProcessorTypes, ItemProcessorComponent } from "../../components/item_processor";
import { ShapeItem } from "../../items/shape_item";
Expand Down Expand Up @@ -190,7 +189,7 @@ export class HUDInteractiveTutorial extends BaseHUDPart {
document.documentElement.setAttribute("data-tutorial-step", hintId);

this.elementGif.style.backgroundImage =
"url('" + cachebust("res/ui/interactive_tutorial.noinline/" + hintId + ".gif") + "')";
"url('res/ui/interactive_tutorial.noinline/" + hintId + ".gif')";
this.element.classList.toggle("animEven");
this.element.classList.toggle("animOdd");
if (hintId) {
Expand Down
5 changes: 2 additions & 3 deletions src/js/platform/browser/sound.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { MusicInstanceInterface, SoundInstanceInterface, SoundInterface, MUSIC, SOUNDS } from "../sound";
import { cachebust } from "../../core/cachebust";
import { createLogger } from "../../core/logging";
import { globalConfig } from "../../core/config";

Expand All @@ -23,7 +22,7 @@ class SoundSpritesContainer {
}
return (this.loadingPromise = new Promise(resolve => {
this.howl = new Howl({
src: cachebust("res/sounds/sfx.mp3"),
src: "res/sounds/sfx.mp3",
sprite: sprites.sprite,
autoplay: false,
loop: false,
Expand Down Expand Up @@ -95,7 +94,7 @@ class MusicInstance extends MusicInstanceInterface {
load() {
return new Promise((resolve, reject) => {
this.howl = new Howl({
src: cachebust("res/sounds/music/" + this.url + ".mp3"),
src: "res/sounds/music/" + this.url + ".mp3",
autoplay: false,
loop: true,
html5: true,
Expand Down
3 changes: 1 addition & 2 deletions src/js/states/about.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TextualGameState } from "../core/textual_game_state";
import { T } from "../translations";
import { THIRDPARTY_URLS } from "../core/config";
import { cachebust } from "../core/cachebust";
import { getLogoSprite } from "../core/utils";

export class AboutState extends TextualGameState {
Expand All @@ -16,7 +15,7 @@ export class AboutState extends TextualGameState {
getMainContentHTML() {
return `
<div class="head">
<img src="${cachebust("res/" + getLogoSprite())}" alt="shapez.io Logo">
<img src="res/${getLogoSprite()}" alt="shapez.io Logo">
</div>
<div class="text">
${T.about.body
Expand Down
7 changes: 3 additions & 4 deletions src/js/states/main_menu.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { cachebust } from "../core/cachebust";
import { globalConfig, THIRDPARTY_URLS } from "../core/config";
import { GameState } from "../core/game_state";
import { DialogWithForm } from "../core/modal_dialog_elements";
Expand Down Expand Up @@ -43,11 +42,11 @@ export class MainMenuState extends GameState {
</div>

<video autoplay muted loop class="fullscreenBackgroundVideo">
<source src="${cachebust("res/bg_render.webm")}" type="video/webm">
<source src="res/bg_render.webm" type="video/webm">
</video>

<div class="logo">
<img src="${cachebust("res/" + getLogoSprite())}" alt="shapez.io Logo"
<img src="res/${getLogoSprite()}" alt="shapez.io Logo"
width="${Math.round((710 / 3) * this.app.getEffectiveUiScale())}"
height="${Math.round((180 / 3) * this.app.getEffectiveUiScale())}"
>
Expand Down Expand Up @@ -132,7 +131,7 @@ export class MainMenuState extends GameState {

<div class="author">
<a class="producerLink" href="https://tobspr.io" target="_blank" title="tobspr Games" rel="follow">
<img src="${cachebust("res/logo-tobspr-games.svg")}" alt="tobspr Games"
<img src="res/logo-tobspr-games.svg" alt="tobspr Games"
height="${25 * 0.8 * this.app.getEffectiveUiScale()}"
width="${82 * 0.8 * this.app.getEffectiveUiScale()}"
>
Expand Down
15 changes: 1 addition & 14 deletions src/js/states/mobile_warning.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { cachebust } from "../core/cachebust";
import { GameState } from "../core/game_state";

export class MobileWarningState extends GameState {
Expand All @@ -9,7 +8,7 @@ export class MobileWarningState extends GameState {
getInnerHTML() {
return `

<img class="logo" src="${cachebust("res/logo.png")}" alt="shapez.io Logo">
<img class="logo" src="res/logo.png" alt="shapez.io Logo">

<p>I'm sorry, but shapez.io is not available on mobile devices yet!</p>
<p>If you have a desktop device, you can get shapez on Steam:</p>
Expand All @@ -27,18 +26,6 @@ export class MobileWarningState extends GameState {
return false;
}

onEnter() {
try {
if (window.gtag) {
window.gtag("event", "click", {
event_category: "ui",
event_label: "mobile_warning",
});
}
} catch (ex) {
console.warn("Failed to track mobile click:", ex);
}
}
onLeave() {
// this.dialogs.cleanup();
}
Expand Down
3 changes: 1 addition & 2 deletions src/js/states/preload.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { CHANGELOG } from "../changelog";
import { cachebust } from "../core/cachebust";
import { globalConfig } from "../core/config";
import { GameState } from "../core/game_state";
import { createLogger } from "../core/logging";
Expand Down Expand Up @@ -283,7 +282,7 @@ export class PreloadState extends GameState {

subElement.innerHTML = `
<div class="logo">
<img src="${cachebust("res/" + getLogoSprite())}" alt="Shapez.io Logo">
<img src="res/getLogoSprite()" alt="Shapez.io Logo">
</div>
<div class="failureInner">
<div class="errorHeader">
Expand Down
Loading