xpine 0.0.32 → 0.0.34

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.
package/README.md CHANGED
@@ -54,13 +54,16 @@ setupEnv also supports AWS secrets manager. Simply add SECRETS_NAME=your_aws_sec
54
54
  - enables cross-origin spa navigation requests (untested)
55
55
 
56
56
  #### Custom events
57
- - spa:updatePageContent
57
+ - spa-update-page-content
58
58
  - sent when the page content has update
59
- - spa:updatePageURL
60
- - send when the page URL has update
59
+ - spa-update-page-url
60
+ - sent when the page URL has update
61
+ - spa-link-click
62
+ - sent when link initially gets clicked and when link is done updating content
63
+ - the "state" value in the event detail will be "start" or "end"
61
64
 
62
65
  #### Custom scripts for certain pages
63
66
 
64
67
  1. Add script to src/public/scripts/pages/your_script.ts
65
68
  2. Import script into page HTML (e.g. <script src="/scripts/pages/your_script.ts">)
66
- 3. To unload event listeners, use `window.addEventListener('spa:updatePageURL', () => { remove event listeners here})` in the code
69
+ 3. To unload event listeners, use `window.addEventListener('spa-update-page-url', () => { remove event listeners here})` in the code
package/TODO CHANGED
@@ -0,0 +1 @@
1
+ - spa:updatePageURL should return all available information about the URL instead of just the href it's going to
@@ -9,11 +9,21 @@ async function updatePageOnLinkClick(e) {
9
9
  e.preventDefault();
10
10
  const targetHref = e?.target?.closest("a")?.getAttribute("href");
11
11
  if (!targetHref) return;
12
- if (isExternalURL(targetHref) && !e?.target?.getAttribute("data-spa-crossorigin")) return;
12
+ const isExternal = isExternalURL(targetHref);
13
+ if (isExternal && !e?.target?.getAttribute("data-spa-crossorigin")) return;
13
14
  e.preventDefault();
14
15
  try {
16
+ if (targetHref === window.location.href) return;
17
+ const newURL = isExternal ? safeParseURL(targetHref) : safeParseURL(window.location.href);
18
+ const startEvent = new CustomEvent("spa-link-click", {
19
+ detail: {
20
+ state: "start",
21
+ href: targetHref,
22
+ url: newURL
23
+ }
24
+ });
25
+ window.dispatchEvent(startEvent);
15
26
  await getNewPageContent(targetHref);
16
- if (targetHref === window.location.pathname) return;
17
27
  window.history.pushState(
18
28
  {
19
29
  type: "link-click",
@@ -22,12 +32,21 @@ async function updatePageOnLinkClick(e) {
22
32
  "",
23
33
  targetHref
24
34
  );
25
- const event = new CustomEvent("spa:updatePageURL", {
35
+ const event = new CustomEvent("spa-update-page-url", {
26
36
  detail: {
27
- href: targetHref
37
+ href: targetHref,
38
+ url: newURL
28
39
  }
29
40
  });
30
41
  window.dispatchEvent(event);
42
+ const endEvent = new CustomEvent("spa-link-click", {
43
+ detail: {
44
+ state: "end",
45
+ href: targetHref,
46
+ url: newURL
47
+ }
48
+ });
49
+ window.dispatchEvent(endEvent);
31
50
  } catch (err) {
32
51
  console.error(err);
33
52
  }
@@ -60,9 +79,10 @@ async function getNewPageContent(href) {
60
79
  replaceDocumentContentsWithLinkResponse();
61
80
  const newDocumentRoot = document.body.querySelector("#xpine-root");
62
81
  for (const script of newScripts) newDocumentRoot.appendChild(script);
63
- const event = new CustomEvent("spa:updatePageContent", {
82
+ const event = new CustomEvent("spa-update-page-content", {
64
83
  detail: {
65
- href
84
+ href,
85
+ url: safeParseURL(res.url)
66
86
  }
67
87
  });
68
88
  window.dispatchEvent(event);
@@ -170,5 +190,12 @@ function isExternalURL(url) {
170
190
  return false;
171
191
  }
172
192
  }
193
+ function safeParseURL(url) {
194
+ try {
195
+ return new URL(url);
196
+ } catch (err) {
197
+ return {};
198
+ }
199
+ }
173
200
  window.addEventListener("DOMContentLoaded", replaceDocumentContentsWithLinkResponse);
174
201
  window.addEventListener("popstate", handleBackButton);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xpine",
3
- "version": "0.0.32",
3
+ "version": "0.0.34",
4
4
  "main": "dist/index.js",
5
5
  "dependencies": {
6
6
  "@aws-sdk/client-secrets-manager": "^3.758.0",