Cache busting of JSON files in webpack

Multi tool use
I have the following code (written in typescript, but could be any JS variant):
this.http.get('configs/config.json').subscribe(...);
Basically, I'm loading a configuration from a local json file. I would like to have cache busting implemented on the file.
Although I can set up my webpack to modify json files by adding a hash suffix, I would also need to modify all the source files which have references to those files. string-replace-loader
might do the job, but doing this feels bit odd.
Additionally, in some cases I don't have access to the code lines that make the http call to resource (third-party plugin for e.g. translation that load something like i18n/[lang].json
so I can't directly modify code and/or name (and thus content hash) is only known in the run-time.
Is there something like URL rewrite for webpack that could solve this?
angular caching webpack
|
show 6 more comments
I have the following code (written in typescript, but could be any JS variant):
this.http.get('configs/config.json').subscribe(...);
Basically, I'm loading a configuration from a local json file. I would like to have cache busting implemented on the file.
Although I can set up my webpack to modify json files by adding a hash suffix, I would also need to modify all the source files which have references to those files. string-replace-loader
might do the job, but doing this feels bit odd.
Additionally, in some cases I don't have access to the code lines that make the http call to resource (third-party plugin for e.g. translation that load something like i18n/[lang].json
so I can't directly modify code and/or name (and thus content hash) is only known in the run-time.
Is there something like URL rewrite for webpack that could solve this?
angular caching webpack
What if you injected a hash so that you getconfigs/config.json?<hash goes here>
? If the hash changes, then it would invalidate the query. You could inject that easily withDefinePlugin
etc.
– Juho Vepsäläinen
Dec 20 '16 at 4:30
Where would I inject this hash to? The ts file that loads it (problem here in described in the third paragraph) or do it by renaming file in webpack (windows server doesn't allow this naming)?
– Miroslav Jonas
Dec 20 '16 at 16:19
DefinePlugin
could do it if you have something likehash = HASH;
and replacedHASH
.. Then you can do'configs/config.json' + hash
as usual.
– Juho Vepsäläinen
Dec 20 '16 at 17:46
That worked, thanks. I just need to find a way to make this HASH depending on the actual file content.
– Miroslav Jonas
Dec 22 '16 at 16:30
That's a harder problem. If it's just a single file, then you can calculate md5 over the file content with Node.js and inject that. Dealing multiple would be trickier. I don't have enough info to give an exact solution.
– Juho Vepsäläinen
Dec 22 '16 at 16:49
|
show 6 more comments
I have the following code (written in typescript, but could be any JS variant):
this.http.get('configs/config.json').subscribe(...);
Basically, I'm loading a configuration from a local json file. I would like to have cache busting implemented on the file.
Although I can set up my webpack to modify json files by adding a hash suffix, I would also need to modify all the source files which have references to those files. string-replace-loader
might do the job, but doing this feels bit odd.
Additionally, in some cases I don't have access to the code lines that make the http call to resource (third-party plugin for e.g. translation that load something like i18n/[lang].json
so I can't directly modify code and/or name (and thus content hash) is only known in the run-time.
Is there something like URL rewrite for webpack that could solve this?
angular caching webpack
I have the following code (written in typescript, but could be any JS variant):
this.http.get('configs/config.json').subscribe(...);
Basically, I'm loading a configuration from a local json file. I would like to have cache busting implemented on the file.
Although I can set up my webpack to modify json files by adding a hash suffix, I would also need to modify all the source files which have references to those files. string-replace-loader
might do the job, but doing this feels bit odd.
Additionally, in some cases I don't have access to the code lines that make the http call to resource (third-party plugin for e.g. translation that load something like i18n/[lang].json
so I can't directly modify code and/or name (and thus content hash) is only known in the run-time.
Is there something like URL rewrite for webpack that could solve this?
angular caching webpack
angular caching webpack
edited Dec 19 '16 at 10:19


jpgrassi
2,44921833
2,44921833
asked Dec 19 '16 at 10:12
Miroslav JonasMiroslav Jonas
1,8231125
1,8231125
What if you injected a hash so that you getconfigs/config.json?<hash goes here>
? If the hash changes, then it would invalidate the query. You could inject that easily withDefinePlugin
etc.
– Juho Vepsäläinen
Dec 20 '16 at 4:30
Where would I inject this hash to? The ts file that loads it (problem here in described in the third paragraph) or do it by renaming file in webpack (windows server doesn't allow this naming)?
– Miroslav Jonas
Dec 20 '16 at 16:19
DefinePlugin
could do it if you have something likehash = HASH;
and replacedHASH
.. Then you can do'configs/config.json' + hash
as usual.
– Juho Vepsäläinen
Dec 20 '16 at 17:46
That worked, thanks. I just need to find a way to make this HASH depending on the actual file content.
– Miroslav Jonas
Dec 22 '16 at 16:30
That's a harder problem. If it's just a single file, then you can calculate md5 over the file content with Node.js and inject that. Dealing multiple would be trickier. I don't have enough info to give an exact solution.
– Juho Vepsäläinen
Dec 22 '16 at 16:49
|
show 6 more comments
What if you injected a hash so that you getconfigs/config.json?<hash goes here>
? If the hash changes, then it would invalidate the query. You could inject that easily withDefinePlugin
etc.
– Juho Vepsäläinen
Dec 20 '16 at 4:30
Where would I inject this hash to? The ts file that loads it (problem here in described in the third paragraph) or do it by renaming file in webpack (windows server doesn't allow this naming)?
– Miroslav Jonas
Dec 20 '16 at 16:19
DefinePlugin
could do it if you have something likehash = HASH;
and replacedHASH
.. Then you can do'configs/config.json' + hash
as usual.
– Juho Vepsäläinen
Dec 20 '16 at 17:46
That worked, thanks. I just need to find a way to make this HASH depending on the actual file content.
– Miroslav Jonas
Dec 22 '16 at 16:30
That's a harder problem. If it's just a single file, then you can calculate md5 over the file content with Node.js and inject that. Dealing multiple would be trickier. I don't have enough info to give an exact solution.
– Juho Vepsäläinen
Dec 22 '16 at 16:49
What if you injected a hash so that you get
configs/config.json?<hash goes here>
? If the hash changes, then it would invalidate the query. You could inject that easily with DefinePlugin
etc.– Juho Vepsäläinen
Dec 20 '16 at 4:30
What if you injected a hash so that you get
configs/config.json?<hash goes here>
? If the hash changes, then it would invalidate the query. You could inject that easily with DefinePlugin
etc.– Juho Vepsäläinen
Dec 20 '16 at 4:30
Where would I inject this hash to? The ts file that loads it (problem here in described in the third paragraph) or do it by renaming file in webpack (windows server doesn't allow this naming)?
– Miroslav Jonas
Dec 20 '16 at 16:19
Where would I inject this hash to? The ts file that loads it (problem here in described in the third paragraph) or do it by renaming file in webpack (windows server doesn't allow this naming)?
– Miroslav Jonas
Dec 20 '16 at 16:19
DefinePlugin
could do it if you have something like hash = HASH;
and replaced HASH
.. Then you can do 'configs/config.json' + hash
as usual.– Juho Vepsäläinen
Dec 20 '16 at 17:46
DefinePlugin
could do it if you have something like hash = HASH;
and replaced HASH
.. Then you can do 'configs/config.json' + hash
as usual.– Juho Vepsäläinen
Dec 20 '16 at 17:46
That worked, thanks. I just need to find a way to make this HASH depending on the actual file content.
– Miroslav Jonas
Dec 22 '16 at 16:30
That worked, thanks. I just need to find a way to make this HASH depending on the actual file content.
– Miroslav Jonas
Dec 22 '16 at 16:30
That's a harder problem. If it's just a single file, then you can calculate md5 over the file content with Node.js and inject that. Dealing multiple would be trickier. I don't have enough info to give an exact solution.
– Juho Vepsäläinen
Dec 22 '16 at 16:49
That's a harder problem. If it's just a single file, then you can calculate md5 over the file content with Node.js and inject that. Dealing multiple would be trickier. I don't have enough info to give an exact solution.
– Juho Vepsäläinen
Dec 22 '16 at 16:49
|
show 6 more comments
1 Answer
1
active
oldest
votes
In webpack we usually rely on the compiler to build the modules and maintain the hashes so chunks are invalidated accordingly when modules and their dependencies change.
If you use file-loader
for the JSON files and add the [hash]
option to add an identifier based on their contents, you could change to something like the following and webpack will handle the rest during builds:
const configFileUrl = require('configs/config.json')
this.http.get(configFileUrl).subscribe(...)
Using loaders is usually the best way to opt in, because this adds modules, enables plugin access and maintains dependencies.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41220230%2fcache-busting-of-json-files-in-webpack%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In webpack we usually rely on the compiler to build the modules and maintain the hashes so chunks are invalidated accordingly when modules and their dependencies change.
If you use file-loader
for the JSON files and add the [hash]
option to add an identifier based on their contents, you could change to something like the following and webpack will handle the rest during builds:
const configFileUrl = require('configs/config.json')
this.http.get(configFileUrl).subscribe(...)
Using loaders is usually the best way to opt in, because this adds modules, enables plugin access and maintains dependencies.
add a comment |
In webpack we usually rely on the compiler to build the modules and maintain the hashes so chunks are invalidated accordingly when modules and their dependencies change.
If you use file-loader
for the JSON files and add the [hash]
option to add an identifier based on their contents, you could change to something like the following and webpack will handle the rest during builds:
const configFileUrl = require('configs/config.json')
this.http.get(configFileUrl).subscribe(...)
Using loaders is usually the best way to opt in, because this adds modules, enables plugin access and maintains dependencies.
add a comment |
In webpack we usually rely on the compiler to build the modules and maintain the hashes so chunks are invalidated accordingly when modules and their dependencies change.
If you use file-loader
for the JSON files and add the [hash]
option to add an identifier based on their contents, you could change to something like the following and webpack will handle the rest during builds:
const configFileUrl = require('configs/config.json')
this.http.get(configFileUrl).subscribe(...)
Using loaders is usually the best way to opt in, because this adds modules, enables plugin access and maintains dependencies.
In webpack we usually rely on the compiler to build the modules and maintain the hashes so chunks are invalidated accordingly when modules and their dependencies change.
If you use file-loader
for the JSON files and add the [hash]
option to add an identifier based on their contents, you could change to something like the following and webpack will handle the rest during builds:
const configFileUrl = require('configs/config.json')
this.http.get(configFileUrl).subscribe(...)
Using loaders is usually the best way to opt in, because this adds modules, enables plugin access and maintains dependencies.
answered Dec 28 '18 at 21:39
Filip DupanovićFilip Dupanović
19.9k96494
19.9k96494
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41220230%2fcache-busting-of-json-files-in-webpack%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
63XjfThWGXdshVJQ,qETJTspNBQvWBIC
What if you injected a hash so that you get
configs/config.json?<hash goes here>
? If the hash changes, then it would invalidate the query. You could inject that easily withDefinePlugin
etc.– Juho Vepsäläinen
Dec 20 '16 at 4:30
Where would I inject this hash to? The ts file that loads it (problem here in described in the third paragraph) or do it by renaming file in webpack (windows server doesn't allow this naming)?
– Miroslav Jonas
Dec 20 '16 at 16:19
DefinePlugin
could do it if you have something likehash = HASH;
and replacedHASH
.. Then you can do'configs/config.json' + hash
as usual.– Juho Vepsäläinen
Dec 20 '16 at 17:46
That worked, thanks. I just need to find a way to make this HASH depending on the actual file content.
– Miroslav Jonas
Dec 22 '16 at 16:30
That's a harder problem. If it's just a single file, then you can calculate md5 over the file content with Node.js and inject that. Dealing multiple would be trickier. I don't have enough info to give an exact solution.
– Juho Vepsäläinen
Dec 22 '16 at 16:49