@hyperspan/framework 1.0.16 → 1.0.17

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperspan/framework",
3
- "version": "1.0.16",
3
+ "version": "1.0.17",
4
4
  "description": "Hyperspan Web Framework",
5
5
  "main": "src/server.ts",
6
6
  "types": "src/server.ts",
@@ -77,6 +77,27 @@ function formSubmitToRoute(e: Event, form: HTMLFormElement, opts: TFormSubmitOpt
77
77
  submitBtn.setAttribute('disabled', 'disabled');
78
78
  }
79
79
 
80
+ function applyResponseHtml(html: string) {
81
+ const isFullDocument = html.includes('<html');
82
+ if (isFullDocument) {
83
+ html = html.replace(/^[\s\uFEFF]*<!DOCTYPE[^>]*>/i, '');
84
+ }
85
+ const target = isFullDocument ? window.document : hsActionTag || form;
86
+ const options = isFullDocument ? undefined : { morphStyle: 'innerHTML' };
87
+
88
+ Idiomorph.morph(target, html, options);
89
+
90
+ if (!isFullDocument) {
91
+ const outerElement = target.querySelector('hs-action');
92
+ if (outerElement) {
93
+ outerElement.replaceWith(...outerElement.childNodes);
94
+ }
95
+ }
96
+
97
+ opts.afterResponse && opts.afterResponse();
98
+ lazyLoadScripts();
99
+ }
100
+
80
101
  fetch(formUrl, { body: formData, method, headers })
81
102
  .then((res: Response) => {
82
103
  // Look for special header that indicates a redirect.
@@ -84,6 +105,17 @@ function formSubmitToRoute(e: Event, form: HTMLFormElement, opts: TFormSubmitOpt
84
105
  if (res.headers.has('X-Redirect-Location')) {
85
106
  const newUrl = res.headers.get('X-Redirect-Location');
86
107
  if (newUrl) {
108
+ const resolved = new URL(newUrl, window.location.href);
109
+
110
+ // If the new URL is the same as the current URL, we can just fetch the new HTML and apply it
111
+ if (resolved.pathname === window.location.pathname) {
112
+ return fetch(resolved.href, {
113
+ headers: { Accept: 'text/html' },
114
+ })
115
+ .then((r) => r.text());
116
+ }
117
+
118
+ // If the new URL is different, we need to redirect the user to the new URL
87
119
  window.location.assign(newUrl);
88
120
  }
89
121
  return '';
@@ -97,17 +129,9 @@ function formSubmitToRoute(e: Event, form: HTMLFormElement, opts: TFormSubmitOpt
97
129
  return;
98
130
  }
99
131
 
100
- const target = content.includes('<html') ? window.document.body : hsActionTag || form;
101
-
102
- Idiomorph.morph(target, content, { morphStyle: 'innerHTML' });
103
-
104
- // Check for nested hs-action elements and remove them if present
105
- const outerElement = target.querySelector('hs-action');
106
- if (outerElement) {
107
- outerElement.replaceWith(...outerElement.childNodes);
108
- }
109
-
110
- opts.afterResponse && opts.afterResponse();
111
- lazyLoadScripts();
132
+ applyResponseHtml(content);
133
+ })
134
+ .catch((error) => {
135
+ console.error('[Hyperspan] Error submitting form action:', error);
112
136
  });
113
137
  }