Skip to content

Commit 9bbbb7b

Browse files
Merge pull request #44 from BerkinAKKAYA/async-fetch-path-name
Without path name for async fetch
2 parents ea2c8b9 + 92595a8 commit 9bbbb7b

File tree

3 files changed

+75
-10
lines changed

3 files changed

+75
-10
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.6.4",
4+
"version": "1.6.5",
55
"author": "<[email protected]>",
66
"license": "MIT",
77
"repository": {

src/core.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export class Core extends Module {
148148

149149
private static fetchGatewayFragment(fragment: IPageFragmentConfig) {
150150
const queryString = this.prepareQueryString(fragment.attributes);
151-
const fragmentRequestUrl = `${fragment.source}${window.location.pathname}${queryString}`;
151+
const fragmentRequestUrl = `${fragment.source}${(fragment.attributes.withoutPathname) ? '' : window.location.pathname}${queryString}`;
152152
return fetch(fragmentRequestUrl, {
153153
headers: {
154154
originalurl: window.location.pathname
@@ -236,7 +236,7 @@ export class Core extends Module {
236236
if (!asset.preLoaded) {
237237
asset.preLoaded = true;
238238
asset.defer = true;
239-
239+
240240
if (asset.dependent) {
241241
asset.dependent.forEach((dependencyName) => {
242242
const dependency = Core.__pageConfiguration.dependencies.filter(dependency => dependency.name === dependencyName);
@@ -258,7 +258,7 @@ export class Core extends Module {
258258
}
259259
});
260260
}
261-
261+
262262
if (loadList.indexOf(asset) === -1) {
263263
loadList.push(asset);
264264
}
@@ -319,4 +319,4 @@ export class Core extends Module {
319319
&& 'IntersectionObserverEntry' in window
320320
&& 'intersectionRatio' in window.IntersectionObserverEntry.prototype;
321321
}
322-
}
322+
}

test/core.spec.ts

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ describe('Module - Core', () => {
233233
Core.config(JSON.stringify(config));
234234
await Core.renderAsyncFragment('test');
235235
await Core.renderAsyncFragment('test');
236-
236+
237237
expect(fetchStub.calledOnce).to.eq(true);
238238
expect(fetchStub.getCall(0).lastArg.headers).to.haveOwnProperty("originalurl");
239239
expect(stubAsyncRenderResponse.calledOnce).to.eq(true);
@@ -288,7 +288,71 @@ describe('Module - Core', () => {
288288
Core.config(JSON.stringify(config));
289289
await Core.renderAsyncFragment('test');
290290
await Core.renderAsyncFragment('test');
291-
291+
292+
expect(fetchStub.calledOnce).to.eq(true);
293+
expect(fetchStub.getCall(0).lastArg.headers).to.haveOwnProperty("originalurl");
294+
expect(stubAsyncRenderResponse.calledOnce).to.eq(true);
295+
});
296+
297+
it('should render async fragment withoutPathname', async () => {
298+
const source = "source/";
299+
const assets = [
300+
{
301+
name: 'bundle1',
302+
dependent: ['vendor1'],
303+
preLoaded: false,
304+
link: 'bundle1.js',
305+
fragment: 'test',
306+
loadMethod: RESOURCE_LOADING_TYPE.ON_PAGE_RENDER,
307+
type: RESOURCE_TYPE.JS
308+
}
309+
] as IPageLibAsset[];
310+
const dependencies = [
311+
{
312+
name: 'vendor1',
313+
link: 'vendor1.js',
314+
preLoaded: false
315+
}
316+
] as IPageLibDependency[];
317+
const config = {
318+
dependencies,
319+
assets,
320+
fragments: [{
321+
name: 'test',
322+
attributes: {
323+
if: "false",
324+
withoutPathname: "true"
325+
},
326+
chunked: true,
327+
clientAsync: true,
328+
clientAsyncForce: undefined,
329+
onDemand: undefined,
330+
criticalCss: undefined,
331+
source,
332+
asyncDecentralized: false
333+
}],
334+
page: 'page',
335+
peers: []
336+
} as IPageLibConfiguration;
337+
338+
const fragmentContainer = global.window.document.createElement('div');
339+
fragmentContainer.setAttribute('puzzle-fragment', 'test');
340+
global.window.document.body.appendChild(fragmentContainer);
341+
342+
const fetchStub = global.fetch as SinonStub;
343+
const stubAsyncRenderResponse = sandbox.stub(Core as any, "asyncRenderResponse").resolves();
344+
345+
Core.config(JSON.stringify(config));
346+
await Core.renderAsyncFragment('test');
347+
await Core.renderAsyncFragment('test');
348+
349+
const fragmentRequestUrl = `${source}?__renderMode=stream&if=false&withoutPathname=true`;
350+
expect(fetchStub.calledWith(fragmentRequestUrl, {
351+
headers: {
352+
originalurl: window.location.pathname
353+
},
354+
credentials: 'include'
355+
})).to.eq(true);
292356
expect(fetchStub.calledOnce).to.eq(true);
293357
expect(fetchStub.getCall(0).lastArg.headers).to.haveOwnProperty("originalurl");
294358
expect(stubAsyncRenderResponse.calledOnce).to.eq(true);
@@ -307,7 +371,7 @@ describe('Module - Core', () => {
307371

308372
Core.config(JSON.stringify(config));
309373
const result = Core.renderAsyncFragment('test');
310-
374+
311375
expect(result).to.be.a('promise');
312376
});
313377

@@ -353,7 +417,8 @@ describe('Module - Core', () => {
353417

354418
Core.config(JSON.stringify(config));
355419
const result = Core.renderAsyncFragment('test');
356-
420+
357421
expect(result).to.be.a('promise');
358422
});
359-
});
423+
424+
});

0 commit comments

Comments
 (0)