Skip to content

Commit

Permalink
CLDR-17270 st: don't use late cache busting for bundle.js
Browse files Browse the repository at this point in the history
- instead, let webpack create a bundle name based on the content's hash, which will truly change when and only when the content changes.
- webpack hook to write the file
- read a manifest.json on the server side to serve that
  • Loading branch information
srl295 committed Feb 2, 2024
1 parent 989104a commit ceef05b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
32 changes: 27 additions & 5 deletions tools/cldr-apps/js/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
const path = require("path");
const fs = require('fs');
const { VueLoaderPlugin } = require("vue-loader");
const { DefinePlugin } = require("webpack");
const { DefinePlugin, Compiler } = require("webpack");

class SurveyToolPlugin {
constructor() {

}
/**
*
* @param {Compiler} compiler
*/
apply(compiler) {
compiler.hooks.afterEmit.tap('SurveyToolPlugin', (compilation) => {
const { assets } = compilation;
const jsfiles = Object.keys(assets).filter(s => /\.js$/.test(s));
const manifestFile = path.resolve(compiler.outputPath, 'manifest.json');
fs.writeFileSync(manifestFile, JSON.stringify({ jsfiles }), 'utf-8');
console.log('# SurveyToolPlugin Wrote: ', manifestFile);
});
}
};

module.exports = (env, argv) => {
const {mode} = argv;
Expand All @@ -12,7 +32,7 @@ module.exports = (env, argv) => {
cacheDirectory: path.resolve(__dirname, '../target/webpack_cache'),
},
output: {
filename: "bundle.js",
filename: "[name].[contenthash].js",
path: path.resolve(__dirname, "..", "src", "main", "webapp", "dist"),
library: "cldrBundle",
libraryTarget: "var",
Expand Down Expand Up @@ -43,7 +63,9 @@ module.exports = (env, argv) => {
// esm bundler flags,
// see <https://github.com/vuejs/vue-next/tree/master/packages/vue#bundler-build-feature-flags>
__VUE_PROD_DEVTOOLS__: JSON.stringify(DEV), // TODO: support dev mode
__VUE_OPTIONS_API__: JSON.stringify(true),
})],
};
__VUE_OPTIONS_API__: JSON.stringify(true),
}),
new SurveyToolPlugin(),
]
};
};
32 changes: 27 additions & 5 deletions tools/cldr-apps/src/main/java/org/unicode/cldr/web/SurveyTool.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package org.unicode.cldr.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
Expand All @@ -14,6 +19,9 @@
import org.unicode.cldr.util.CLDRURLS;
import org.unicode.cldr.util.CldrUtility;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class SurveyTool extends HttpServlet {
static final Logger logger = SurveyLog.forClass(SurveyTool.class);

Expand Down Expand Up @@ -242,6 +250,12 @@ public static void includeMonitoring(Writer out) throws IOException {
}
}

static private final Gson gson = new GsonBuilder().setPrettyPrinting().create();

private final class STManifest {
public String jsfiles[];
}

/**
* Write the script tags for Survey Tool JavaScript files
*
Expand All @@ -253,15 +267,23 @@ public static void includeMonitoring(Writer out) throws IOException {
public static void includeJavaScript(HttpServletRequest request, Writer out)
throws IOException, JSONException {
includeMonitoring(out);
// Load the big bundle
out.write(
"<script src=\"dist/bundle"
+ getCacheBustingExtension(request)
+ ".js\"></script>\n");

// use WebPack-built manifest.json to include all chunks.
// ideally this would all come from a static .html file built by WebPack.
// TODO https://unicode-org.atlassian.net/browse/CLDR-17353
try (final InputStream is = request.getServletContext().getResourceAsStream("dist/manifest.json");
final Reader r = new InputStreamReader(is, StandardCharsets.UTF_8);) {
for(final String f : gson.fromJson(r, STManifest.class).jsfiles) {
out.write("<script src=\"" + request.getContextPath() + "/dist/" + f.toString() + "\"></script>\n");
}
}

includeJqueryJavaScript(request, out);
includeCldrJavaScript(request, out);
}



private static void includeJqueryJavaScript(HttpServletRequest request, Writer out)
throws IOException {
// Per https://en.wikipedia.org/wiki/JQuery#Release_history --
Expand Down

0 comments on commit ceef05b

Please sign in to comment.