Skip to content

Commit 56190d5

Browse files
authored
Merge pull request #9 from puzzle-js/feature/render-async-fragment
renderAsyncFragment added to core
2 parents 37ed5a4 + 5604d91 commit 56190d5

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@puzzle-js/client-lib",
33
"main": "dist/index.js",
4-
"version": "1.2.4",
4+
"version": "1.3.0",
55
"author": "<[email protected]>",
66
"license": "MIT",
77
"repository": {

src/core.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,16 @@ export class Core extends Module {
9595
}
9696

9797
private static asyncLoadFragment(fragment: IPageFragmentConfig) {
98+
if (fragment.asyncLoaded) return;
99+
fragment.asyncLoaded = true;
98100
const queryString = this.prepareQueryString(fragment.attributes);
99-
const key = `${fragment.source}${location.pathname}${queryString}`;
101+
const key = `${fragment.source}${window.location.pathname}${queryString}`;
100102

101103
if (!fragment.asyncDecentralized) {
102104
return this.fetchGatewayFragment(fragment)
103105
.then(res => this.asyncRenderResponse(fragment, res));
104106
}
105107

106-
107108
Core.gun
108109
.get(key, (gunResponse: any) => {
109110
if (gunResponse.err || !gunResponse.put) {
@@ -141,7 +142,7 @@ export class Core extends Module {
141142

142143
private static fetchGatewayFragment(fragment: IPageFragmentConfig) {
143144
const queryString = this.prepareQueryString(fragment.attributes);
144-
const fragmentRequestUrl = `${fragment.source}${location.pathname}${queryString}`;
145+
const fragmentRequestUrl = `${fragment.source}${window.location.pathname}${queryString}`;
145146
return fetch(fragmentRequestUrl, {
146147
credentials: 'include'
147148
})
@@ -183,7 +184,7 @@ export class Core extends Module {
183184
}
184185

185186
private static prepareQueryString(fragmentAttributes: Record<string, string>) {
186-
const attributes = Object.assign(location.search.slice(1).split('&').reduce((dict: { [name: string]: string }, i) => {
187+
const attributes = Object.assign(window.location.search.slice(1).split('&').reduce((dict: { [name: string]: string }, i) => {
187188
const [key, value] = i.split('=');
188189
if (typeof value !== "undefined") {
189190
dict[key] = value;
@@ -269,6 +270,18 @@ export class Core extends Module {
269270
});
270271
}
271272

273+
static renderAsyncFragment(fragmentName: string) {
274+
const fragment = this.__pageConfiguration.fragments.find(item => item.name === fragmentName);
275+
if (fragment) {
276+
const selector = this.getFragmentContainerSelector(fragment, "main");
277+
const fragmentContainer = window.document.querySelector(selector);
278+
if (fragmentContainer ) {
279+
if (this.observer) this.observer.unobserve(fragmentContainer);
280+
return this.asyncLoadFragment(fragment);
281+
}
282+
}
283+
}
284+
272285
private static isIntersectionObserverSupported() {
273286
return 'IntersectionObserver' in window
274287
&& 'IntersectionObserverEntry' in window

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export interface IPageFragmentConfig {
3434
asyncDecentralized: boolean;
3535
attributes: { [name: string]: string };
3636
source: string | undefined;
37+
asyncLoaded?: boolean;
3738
}
3839

3940
export interface IPageLibDependency {

test/core.spec.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ declare var global: Global;
2929
describe('Module - Core', () => {
3030
beforeEach(() => {
3131
global.window = (new JSDOM(``, {runScripts: "outside-only"})).window;
32-
sandbox.verifyAndRestore();
3332
});
3433

3534
afterEach(() => {
3635
delete global.window;
3736
PuzzleJs.clearListeners();
37+
sandbox.verifyAndRestore();
3838
(Core as any)._pageConfiguration = undefined;
3939
});
4040

@@ -170,7 +170,7 @@ describe('Module - Core', () => {
170170
[]);
171171
});
172172

173-
it('should create true load queue for js assets excluding conditional fragments', function () {
173+
it('should render async fragment', async () => {
174174
const assets = [
175175
{
176176
name: 'bundle1',
@@ -198,19 +198,26 @@ describe('Module - Core', () => {
198198
if: "false"
199199
},
200200
chunked: true,
201-
clientAsync: false,
201+
clientAsync: true,
202202
source: undefined,
203203
asyncDecentralized: false
204204
}],
205205
page: 'page',
206206
peers: []
207207
} as IPageLibConfiguration;
208208

209-
const mockLoadJsSeries = sandbox.mock(AssetHelper);
209+
const fragmentContainer = global.window.document.createElement('div');
210+
fragmentContainer.setAttribute('puzzle-fragment', 'test');
211+
global.window.document.body.appendChild(fragmentContainer);
212+
213+
const stubFetchGatewayFragment = sandbox.stub(Core as any, "fetchGatewayFragment").resolves();
214+
const stubAsyncRenderResponse = sandbox.stub(Core as any, "asyncRenderResponse").resolves();
210215

211216
Core.config(JSON.stringify(config));
212-
Core.pageLoaded();
217+
await Core.renderAsyncFragment('test');
218+
await Core.renderAsyncFragment('test');
213219

214-
mockLoadJsSeries.expects("loadJsSeries").calledWith([]);
220+
expect(stubFetchGatewayFragment.calledOnce).to.eq(true);
221+
expect(stubAsyncRenderResponse.calledOnce).to.eq(true);
215222
});
216223
});

0 commit comments

Comments
 (0)