@csedl/svelte-on-rails 12.1.0 → 12.1.2

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
@@ -22,4 +22,4 @@ with the properties given by the ruby gem.
22
22
 
23
23
  Please follow [svelte-on-rails docs](https://svelte-on-rails.dev) for more information.
24
24
 
25
- The repository is shared with the gem [svelte-on-rails](https://gitlab.com/sedl/svelte-on-rails) (monorepo).
25
+ The repository is shared with the gem [SOR](https://gitlab.com/sedl/svelte-on-rails) (monorepo).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@csedl/svelte-on-rails",
3
- "version": "12.1.0",
3
+ "version": "12.1.2",
4
4
  "description": "Client-side runtime for svelte-on-rails: hydration, SSR build support & Turbo Stream actions",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/src/config.js CHANGED
@@ -25,7 +25,7 @@ export const SvelteOnRails = {
25
25
  const loader = components[componentKey];
26
26
 
27
27
  if (!loader) {
28
- throw new Error(`[svelte-on-rails] Component not found: «${componentKey}»\nAvailable imported Components are:\n\n+++\n • ${Object.keys(components).join('\n • ')}\n+++\n`);
28
+ throw new Error(`[SOR] Component not found: «${componentKey}»\nAvailable imported Components are:\n\n+++\n • ${Object.keys(components).join('\n • ')}\n+++\n`);
29
29
  //return;
30
30
  } else {
31
31
  debugLog(`Component found: ${componentKey}`);
@@ -35,23 +35,23 @@ export const SvelteOnRails = {
35
35
 
36
36
  let comp;
37
37
  if (loader.prototype) {
38
- console.warn(`[svelte-on-rails] Components should be lazy loaded. Did you use vite.meta.glob(..., {eager: true})?`);
38
+ console.warn(`[SOR] Components should be lazy loaded. Did you use vite.meta.glob(..., {eager: true})?`);
39
39
  comp = loader;
40
40
  } else {
41
41
  comp = await loader();
42
42
  if (typeof comp !== 'function') {
43
- throw new Error(`[svelte-on-rails] Component is not a function: ${componentKey}\nYou may need to use vite.meta.glob(..., {...import: "default"})`);
43
+ throw new Error(`[SOR] Component is not a function: ${componentKey}\nYou may need to use vite.meta.glob(..., {...import: "default"})`);
44
44
  }
45
45
  }
46
46
 
47
47
  // finish
48
48
 
49
- debugLog(`[svelte-on-rails:debug] Component loaded:`, comp);
49
+ debugLog(`Component loaded:`, comp);
50
50
  return comp;
51
51
  },
52
52
 
53
53
  set lazyComponents(value) {
54
- debugLog(`[svelte-on-rails:debug] lazyComponents set:`, value);
54
+ debugLog(`lazyComponents set:`, value);
55
55
  _lazyComponents = value;
56
56
  },
57
57
 
@@ -3,9 +3,6 @@ import {debugLog} from "../logger.js";
3
3
  import {SvelteOnRails} from "../config.js";
4
4
  import {componentRenderError} from "../utils.js";
5
5
 
6
- // Store for tracking initialized Svelte component instances
7
- const svelteInstances = new WeakMap();
8
-
9
6
  export async function initializeSvelteComponent(element) {
10
7
 
11
8
  // Decide mount vs hydrate
@@ -14,7 +11,7 @@ export async function initializeSvelteComponent(element) {
14
11
  // fetch the component path
15
12
  const componentKey = element.getAttribute("data-component")?.trim();
16
13
  if (!componentKey) {
17
- console.warn("[svelte-on-rails] Missing data-component attribute");
14
+ console.warn("[SOR] Missing data-component attribute");
18
15
  return;
19
16
  }
20
17
 
@@ -31,7 +28,7 @@ export async function initializeSvelteComponent(element) {
31
28
  try {
32
29
  props = JSON.parse(propsString);
33
30
  } catch (e) {
34
- console.error(`[svelte-on-rails] Error parsing data-props for ${componentKey}:`, e);
31
+ console.error(`[SOR] Error parsing data-props for ${componentKey}:`, e);
35
32
  }
36
33
  }
37
34
 
@@ -45,13 +42,15 @@ export async function initializeSvelteComponent(element) {
45
42
  instance = hydrate(Component, {target: element, props});
46
43
  }
47
44
 
48
- // Preparing for cleanup
49
- svelteInstances.set(element, instance);
50
-
51
45
  element.setAttribute("data-svelte-status", "initialized");
52
- debugLog(`Success: ${action} ${componentKey}`);
46
+ debugLog(`Success: ${action} ${componentKey}`, instance);
47
+ return {
48
+ instance: instance,
49
+ status: "SUCCESS",
50
+ componentName: componentKey,
51
+ };
53
52
  } catch (e) {
54
- console.error(`[svelte-on-rails] Failed to ${action} ${componentKey}:`, e);
53
+ console.error(`[SOR] Failed to ${action} ${componentKey}:`, e);
55
54
  element.setAttribute("data-svelte-status", "error");
56
55
 
57
56
  element.innerHTML = componentRenderError(componentKey, e);
@@ -1,25 +1,32 @@
1
1
  import {Controller} from "@hotwired/stimulus"
2
2
  import {initializeSvelteComponent} from "./initializeSvelteComponent.js";
3
- import {cleanupSvelteComponent} from "./cleanupSvelteComponent.js";
4
3
  import {debugLog} from "../logger.js";
4
+ import {unmount} from "svelte";
5
5
 
6
6
  export default class extends Controller {
7
7
 
8
+ svelteInstance = {};
9
+
8
10
  connect() {
9
11
 
10
12
  const stat = this.element.getAttribute('data-svelte-status');
11
13
  if (stat === 'do-not-hydrate-me') {
12
14
  debugLog(`Skipping hydration of component ${this.element.getAttribute('data-component')}`);
13
15
  return
14
- };
16
+ }
17
+ ;
15
18
 
16
- initializeSvelteComponent(this.element, false).then(r => {});
19
+ initializeSvelteComponent(this.element, false).then(r => {
20
+ this.svelteInstance = r
21
+ debugLog('HYDRATED', r)
22
+ });
17
23
 
18
24
  }
19
25
 
20
26
  disconnect() {
21
-
22
- cleanupSvelteComponent(this.element, false);
27
+ const name = this.svelteInstance.componentName.split('/').pop();
28
+ const r = unmount(this.svelteInstance.instance);
29
+ debugLog(`disconnected ${name} ${typeof this.svelteInstance.instance}`, r)
23
30
 
24
31
  }
25
32
 
package/src/logger.js CHANGED
@@ -11,14 +11,14 @@ export function streamDebugLog(message, object, object2) {
11
11
  function log(namespace, message, object, object2) {
12
12
  if (SvelteOnRails.debug) {
13
13
 
14
- const nmsp = (namespace ? `:${namespace}` : '');
14
+ const nmsp = (namespace ? ` ${namespace}:` : '');
15
15
 
16
16
  if (object2) {
17
- console.log(`[svelte-on-rails${nmsp}] ${message}`, object, object2);
17
+ console.log(`[SOR]${nmsp} ${message}`, object, object2);
18
18
  } else if (object) {
19
- console.log(`[svelte-on-rails${nmsp}] ${message}`, object);
19
+ console.log(`[SOR]${nmsp} ${message}`, object);
20
20
  } else {
21
- console.log(`[svelte-on-rails${nmsp}] ${message}`);
21
+ console.log(`[SOR]${nmsp} ${message}`);
22
22
  }
23
23
  }
24
24
  }
package/src/ssr/render.js CHANGED
@@ -9,13 +9,13 @@ const compiledComponentPath = process.argv[2];
9
9
 
10
10
  (async () => {
11
11
  console.log(`${performance.now()}<time>`);
12
- console.log(`[svelte-on-rails:debug] awaiting load component => «${compiledComponentPath}»`);
12
+ console.log(`[SOR] awaiting load component => «${compiledComponentPath}»`);
13
13
  const compiledComponent = await loadComponentModule(compiledComponentPath);
14
14
 
15
- console.log(`[svelte-on-rails:debug] Component loaded: «${compiledComponent}»`);
15
+ console.log(`[SOR] Component loaded: «${compiledComponent}»`);
16
16
 
17
17
  const props = await readPropsFromStdin();
18
- console.log(`[svelte-on-rails:debug] props read: «${JSON.stringify(props)}»`);
18
+ console.log(`[SOR] props read: «${JSON.stringify(props)}»`);
19
19
 
20
20
  try {
21
21
  const { body, head } = render(compiledComponent, { props });
@@ -26,7 +26,7 @@ const compiledComponentPath = process.argv[2];
26
26
  head: head || '',
27
27
  };
28
28
  console.log(`<time>${performance.now()}`);
29
- console.log('[svelte-on-rails:successful-json-response]' + JSON.stringify(res));
29
+ console.log('$$SuccessfulResponseSeparator$$' + JSON.stringify(res));
30
30
  } catch (error) {
31
31
 
32
32
  const res = {
@@ -36,7 +36,7 @@ const compiledComponentPath = process.argv[2];
36
36
  };
37
37
 
38
38
  console.log(`<time>${performance.now()}`);
39
- console.log('[svelte-on-rails:successful-json-response]' + JSON.stringify(res));
39
+ console.log('[SOR] successful json response: ' + JSON.stringify(res));
40
40
  }
41
41
  })();
42
42
 
@@ -6,7 +6,7 @@ export function addComponentStreamListener(node, callback) {
6
6
  const wrapper = node.closest('.svelte-component');
7
7
 
8
8
  if (!wrapper) {
9
- console.error('[svelte-on-rails:stream] No wrapping element with class .svelte-component found!');
9
+ console.error('[SOR] No wrapping element with class .svelte-component found!');
10
10
  return;
11
11
  }
12
12
 
@@ -6,7 +6,7 @@ export function dispatchSvelteStreamEvent(args) {
6
6
  const argKeys = Object.keys(args);
7
7
  const invalidKeys = argKeys.filter(key => !allowedKeys.includes(key));
8
8
  if (invalidKeys.length > 0) {
9
- throw new Error(`[svelte-on-rails:dispatch-event] Invalid keys found: ${invalidKeys.join(', ')}. Allowed keys are: ${allowedKeys.join(', ')}.`);
9
+ throw new Error(`[SOR] Invalid keys found: ${invalidKeys.join(', ')}. Allowed keys are: ${allowedKeys.join(', ')}.`);
10
10
  }
11
11
 
12
12
  streamDebugLog(`parsed arguments:`, args)
@@ -1,29 +0,0 @@
1
- import { unmount } from 'svelte';
2
- import { debugLog } from "../logger.js";
3
-
4
- // Store for tracking initialized Svelte component instances
5
- const svelteInstances = new WeakMap();
6
-
7
- /**
8
- * Cleans up all initialized Svelte component instances.
9
- */
10
- export function cleanupSvelteComponent(element) {
11
- try {
12
- const instance = svelteInstances.get(element);
13
- if (instance) {
14
- unmount(instance).then(()=>{
15
- debugLog(`Successfully unmounted Svelte component for element:`, element);
16
- }); // Destroy the Svelte component instance in Svelte 5
17
- debugLog(`Unmounted Svelte component for element:`, element);
18
- element.removeAttribute('data-svelte-initialized'); // Reset initialized state
19
- svelteInstances.delete(element); // Remove from WeakMap
20
- } else {
21
- let comps = document.getElementsByClassName('svelte-component')
22
- debugLog(`Actual ${comps.length} components on the page. No Svelte instance found`, element);
23
- }
24
- } catch (e) {
25
- console.error(`[svelte-on-rails:stream] Error unmounting Svelte component for element:`, e);
26
- // Fallback: Clear element content to prevent stale content
27
- element.innerHTML = '';
28
- }
29
- }