@jsenv/core 37.0.5 → 37.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,24 +1,17 @@
1
- export const jsenvPluginHotSearchParam = () => {
2
- const shouldInjectHotSearchParam = (reference) => {
3
- if (reference.isImplicit) {
4
- return false;
5
- }
6
- if (reference.original && reference.original.searchParams.has("hot")) {
7
- return true;
8
- }
9
- // parent is using ?hot -> propagate
10
- const { ownerUrlInfo } = reference;
11
- const lastReference = ownerUrlInfo.context.reference;
12
- if (
13
- lastReference &&
14
- lastReference.original &&
15
- lastReference.original.searchParams.has("hot")
16
- ) {
17
- return true;
18
- }
19
- return false;
20
- };
1
+ /*
2
+ * When client wants to hot reload, it wants to be sure it can reach the server
3
+ * and bypass any cache. This is done thanks to "hot" search param
4
+ * being injected by the client: file.js?hot=Date.now()
5
+ * When it happens server must:
6
+ * 1. Consider it's a regular request to "file.js" and not a variation
7
+ * of it (not like file.js?as_js_classic that creates a separate urlInfo)
8
+ * -> This is done by redirectReference deleting the search param.
9
+ *
10
+ * 2. Inject ?hot= into all urls referenced by this one
11
+ * -> This is done by transformReferenceSearchParams
12
+ */
21
13
 
14
+ export const jsenvPluginHotSearchParam = () => {
22
15
  return {
23
16
  name: "jsenv:hot_search_param",
24
17
  appliesDuring: "dev",
@@ -32,20 +25,45 @@ export const jsenvPluginHotSearchParam = () => {
32
25
  // We get rid of this params so that urlGraph and other parts of the code
33
26
  // recognize the url (it is not considered as a different url)
34
27
  urlObject.searchParams.delete("hot");
35
- urlObject.searchParams.delete("v");
36
28
  return urlObject.href;
37
29
  },
38
30
  transformReferenceSearchParams: (reference) => {
39
- if (!shouldInjectHotSearchParam(reference)) {
31
+ if (reference.isImplicit) {
32
+ return null;
33
+ }
34
+ if (reference.original && reference.original.searchParams.has("hot")) {
35
+ return {
36
+ hot: reference.original.searchParams.get("hot"),
37
+ };
38
+ }
39
+ const request = reference.ownerUrlInfo.context.request;
40
+ const parentHotParam = request ? request.searchParams.get("hot") : null;
41
+ if (!parentHotParam) {
40
42
  return null;
41
43
  }
44
+ // At this stage the parent is using ?hot and we are going to decide if
45
+ // we propagate the search param to child.
42
46
  const referencedUrlInfo = reference.urlInfo;
43
- if (!referencedUrlInfo.modifiedTimestamp) {
47
+ const { modifiedTimestamp, dereferencedTimestamp } = referencedUrlInfo;
48
+ if (!modifiedTimestamp && !dereferencedTimestamp) {
44
49
  return null;
45
50
  }
51
+ // The goal is to send an url that will bypass client (the browser) cache
52
+ // more precisely the runtime cache of js modules, but also any http cache
53
+ // that could prevent re-execution of js code
54
+ // In order to achieve this, this plugin inject ?hot=timestamp
55
+ // - The browser will likely not have it in cache
56
+ // and refetch lastest version from server + re-execute it
57
+ // - If the browser have it in cache, he will not get it from server
58
+ // We use the latest timestamp to ensure it's fresh
59
+ // The dereferencedTimestamp is needed because when a js module is re-referenced
60
+ // browser must re-execute it, even if the code is not modified
61
+ const latestTimestamp =
62
+ dereferencedTimestamp && modifiedTimestamp
63
+ ? Math.max(dereferencedTimestamp, modifiedTimestamp)
64
+ : dereferencedTimestamp || modifiedTimestamp;
46
65
  return {
47
- hot: "",
48
- v: referencedUrlInfo.modifiedTimestamp,
66
+ hot: latestTimestamp,
49
67
  };
50
68
  },
51
69
  };
@@ -75,7 +75,6 @@ const asUrlWithoutHotSearchParam = (url) => {
75
75
  const urlObject = new URL(url);
76
76
  if (urlObject.searchParams.has("hot")) {
77
77
  urlObject.searchParams.delete("hot");
78
- urlObject.searchParams.delete("v");
79
78
  return urlObject.href;
80
79
  }
81
80
  return url;
@@ -33,9 +33,7 @@ export const getCorePlugins = ({
33
33
  transpilation = true,
34
34
  inlining = true,
35
35
 
36
- clientAutoreload = false,
37
- clientFileChangeCallbackList,
38
- clientFilesPruneCallbackList,
36
+ clientAutoreload,
39
37
  cacheControl,
40
38
  scenarioPlaceholders = true,
41
39
  ribbon = true,
@@ -46,9 +44,6 @@ export const getCorePlugins = ({
46
44
  if (supervisor === true) {
47
45
  supervisor = {};
48
46
  }
49
- if (clientAutoreload === true) {
50
- clientAutoreload = {};
51
- }
52
47
  if (ribbon === true) {
53
48
  ribbon = {};
54
49
  }
@@ -85,14 +80,8 @@ export const getCorePlugins = ({
85
80
  jsenvPluginNodeRuntime({ runtimeCompat }),
86
81
 
87
82
  jsenvPluginImportMetaHot(),
88
- ...(clientAutoreload
89
- ? [
90
- jsenvPluginAutoreload({
91
- ...clientAutoreload,
92
- clientFileChangeCallbackList,
93
- clientFilesPruneCallbackList,
94
- }),
95
- ]
83
+ ...(clientAutoreload && clientAutoreload.enabled
84
+ ? [jsenvPluginAutoreload(clientAutoreload)]
96
85
  : []),
97
86
  ...(cacheControl ? [jsenvPluginCacheControl(cacheControl)] : []),
98
87
  ...(ribbon ? [jsenvPluginRibbon({ rootDirectoryUrl, ...ribbon })] : []),