Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(custom-elements): hydrate on client side #5317

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isOutputTargetHydrate } from '@utils';

import type * as d from '../../../declarations';
import { getBuildFeatures, updateBuildConditionals } from '../../app-core/app-data';

/**
* Get build conditions appropriate for the `dist-custom-elements` Output
* Target, including disabling lazy loading and hydration.
Expand All @@ -17,9 +18,10 @@ export const getCustomElementsBuildConditionals = (
// then the default in "import { BUILD, NAMESPACE } from '@stencil/core/internal/app-data'"
// needs to have the static build conditionals set for the custom elements build
const build = getBuildFeatures(cmps) as d.BuildConditionals;
const hasHydrateOutputTargets = config.outputTargets.some(isOutputTargetHydrate);

build.lazyLoad = false;
build.hydrateClientSide = false;
build.hydrateClientSide = hasHydrateOutputTargets;
build.hydrateServerSide = false;
build.asyncQueue = config.taskQueue === 'congestionAsync';
build.taskQueue = config.taskQueue !== 'immediate';
Expand Down
45 changes: 45 additions & 0 deletions src/compiler/output-targets/test/build-conditionals.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ describe('build-conditionals', () => {
});

describe('getCustomElementsBuildConditionals', () => {
it('default', () => {
const { config } = validateConfig(userConfig, mockLoadConfigInit());
const bc = getCustomElementsBuildConditionals(config, cmps);
expect(bc).toMatchObject({
lazyLoad: false,
hydrateClientSide: false,
hydrateServerSide: false,
});
});

it('taskQueue async', () => {
userConfig.taskQueue = 'async';
const { config } = validateConfig(userConfig, mockLoadConfigInit());
Expand Down Expand Up @@ -49,9 +59,28 @@ describe('build-conditionals', () => {
expect(bc.taskQueue).toBe(true);
expect(config.taskQueue).toBe('async');
});

it('hydrateClientSide true', () => {
const hydrateOutputTarget: d.OutputTargetHydrate = {
type: 'dist-hydrate-script',
};
userConfig.outputTargets = [hydrateOutputTarget];
const { config } = validateConfig(userConfig, mockLoadConfigInit());
const bc = getCustomElementsBuildConditionals(config, cmps);
expect(bc.hydrateClientSide).toBe(true);
});
});

describe('getLazyBuildConditionals', () => {
it('default', () => {
const { config } = validateConfig(userConfig, mockLoadConfigInit());
const bc = getLazyBuildConditionals(config, cmps);
expect(bc).toMatchObject({
lazyLoad: true,
hydrateServerSide: false,
});
});

it('taskQueue async', () => {
userConfig.taskQueue = 'async';
const { config } = validateConfig(userConfig, mockLoadConfigInit());
Expand Down Expand Up @@ -99,5 +128,21 @@ describe('build-conditionals', () => {
const bc = getLazyBuildConditionals(config, cmps);
expect(bc.transformTagName).toBe(true);
});

it('hydrateClientSide default', () => {
const { config } = validateConfig(userConfig, mockLoadConfigInit());
const bc = getLazyBuildConditionals(config, cmps);
expect(bc.hydrateClientSide).toBe(false);
});

it('hydrateClientSide true', () => {
const hydrateOutputTarget: d.OutputTargetHydrate = {
type: 'dist-hydrate-script',
};
userConfig.outputTargets = [hydrateOutputTarget];
const { config } = validateConfig(userConfig, mockLoadConfigInit());
const bc = getLazyBuildConditionals(config, cmps);
expect(bc.hydrateClientSide).toBe(true);
});
});
});
3 changes: 2 additions & 1 deletion src/runtime/initialize-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export const initializeComponent = async (
// Let the runtime know that the component has been initialized
hostRef.$flags$ |= HOST_FLAGS.hasInitializedComponent;

if (BUILD.lazyLoad || BUILD.hydrateClientSide) {
const bundleId = cmpMeta.$lazyBundleId$;
if ((BUILD.lazyLoad || BUILD.hydrateClientSide) && bundleId) {
// lazy loaded components
// request the component's implementation to be
// wired up with the host element
Expand Down
Loading