Skip to content

Commit f6e28f0

Browse files
chore(angular): sb-blok directive destroyRef added for proper unmount
1 parent 5c1d082 commit f6e28f0

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

packages/angular/projects/sdk/src/lib/sb-blok.directive.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { Directive, input, inject, ViewContainerRef, effect, ComponentRef } from '@angular/core';
1+
import { Directive, input, ViewContainerRef, effect, ComponentRef } from '@angular/core';
22
import type { SbBlokData } from '@storyblok/js';
33
import { StoryblokComponentResolver } from './sb-blok.feature';
44
import { storyblokEditable } from '@storyblok/live-preview';
5+
import { DestroyRef, inject } from '@angular/core';
6+
57
/**
68
* Directive that dynamically renders a Storyblok component based on the blok data.
79
* Supports both eager and lazy-loaded components for optimal bundle size.
@@ -42,7 +44,7 @@ export class SbBlokDirective {
4244
readonly sbBlok = input.required<SbBlokData | null | undefined>();
4345

4446
private componentRef: ComponentRef<unknown> | null = null;
45-
47+
private destroyRef = inject(DestroyRef);
4648
constructor() {
4749
effect(() => {
4850
this.render(this.sbBlok());
@@ -65,6 +67,9 @@ export class SbBlokDirective {
6567

6668
const Component = await this.resolver.resolve(blok.component);
6769

70+
// IMPORTANT: stop if directive already destroyed
71+
if (this.destroyRef.destroyed) return;
72+
6873
if (!Component) {
6974
this.viewContainerRef.clear();
7075
return;

packages/angular/projects/ssr-playground/src/app/app.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import { storyblokComponents } from './storyblok.components';
1212

1313
const sbConfig: StoryblokClientConfig = {
14-
accessToken: 'nVjy7VdMK6ObZxyDfTWNwgtt',
14+
accessToken: 'OurklwV5XsDJTIE1NJaD2wtt',
1515
region: 'eu',
1616
inlineRelations: true,
1717
};

0 commit comments

Comments
 (0)