xpine 0.0.32 → 0.0.33

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,13 @@ 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
59
+ - spa-update-page-url
60
60
  - send when the page URL has update
61
61
 
62
62
  #### Custom scripts for certain pages
63
63
 
64
64
  1. Add script to src/public/scripts/pages/your_script.ts
65
65
  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
66
+ 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,7 +9,8 @@ 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 {
15
16
  await getNewPageContent(targetHref);
@@ -22,9 +23,10 @@ async function updatePageOnLinkClick(e) {
22
23
  "",
23
24
  targetHref
24
25
  );
25
- const event = new CustomEvent("spa:updatePageURL", {
26
+ const event = new CustomEvent("spa-update-page-url", {
26
27
  detail: {
27
- href: targetHref
28
+ href: targetHref,
29
+ url: isExternal ? safeParseURL(targetHref) : safeParseURL(window.location.href)
28
30
  }
29
31
  });
30
32
  window.dispatchEvent(event);
@@ -60,9 +62,10 @@ async function getNewPageContent(href) {
60
62
  replaceDocumentContentsWithLinkResponse();
61
63
  const newDocumentRoot = document.body.querySelector("#xpine-root");
62
64
  for (const script of newScripts) newDocumentRoot.appendChild(script);
63
- const event = new CustomEvent("spa:updatePageContent", {
65
+ const event = new CustomEvent("spa-update-page-content", {
64
66
  detail: {
65
- href
67
+ href,
68
+ url: safeParseURL(res.url)
66
69
  }
67
70
  });
68
71
  window.dispatchEvent(event);
@@ -170,5 +173,12 @@ function isExternalURL(url) {
170
173
  return false;
171
174
  }
172
175
  }
176
+ function safeParseURL(url) {
177
+ try {
178
+ return new URL(url);
179
+ } catch (err) {
180
+ return {};
181
+ }
182
+ }
173
183
  window.addEventListener("DOMContentLoaded", replaceDocumentContentsWithLinkResponse);
174
184
  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.33",
4
4
  "main": "dist/index.js",
5
5
  "dependencies": {
6
6
  "@aws-sdk/client-secrets-manager": "^3.758.0",