Skip to content

Commit

Permalink
Merge pull request #44 from BerkinAKKAYA/async-fetch-path-name
Browse files Browse the repository at this point in the history
Without path name for async fetch
  • Loading branch information
cihadhoruzoglu authored Nov 15, 2022
2 parents ea2c8b9 + 92595a8 commit 9bbbb7b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@puzzle-js/client-lib",
"main": "dist/index.js",
"version": "1.6.4",
"version": "1.6.5",
"author": "<[email protected]>",
"license": "MIT",
"repository": {
Expand Down
8 changes: 4 additions & 4 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class Core extends Module {

private static fetchGatewayFragment(fragment: IPageFragmentConfig) {
const queryString = this.prepareQueryString(fragment.attributes);
const fragmentRequestUrl = `${fragment.source}${window.location.pathname}${queryString}`;
const fragmentRequestUrl = `${fragment.source}${(fragment.attributes.withoutPathname) ? '' : window.location.pathname}${queryString}`;
return fetch(fragmentRequestUrl, {
headers: {
originalurl: window.location.pathname
Expand Down Expand Up @@ -236,7 +236,7 @@ export class Core extends Module {
if (!asset.preLoaded) {
asset.preLoaded = true;
asset.defer = true;

if (asset.dependent) {
asset.dependent.forEach((dependencyName) => {
const dependency = Core.__pageConfiguration.dependencies.filter(dependency => dependency.name === dependencyName);
Expand All @@ -258,7 +258,7 @@ export class Core extends Module {
}
});
}

if (loadList.indexOf(asset) === -1) {
loadList.push(asset);
}
Expand Down Expand Up @@ -319,4 +319,4 @@ export class Core extends Module {
&& 'IntersectionObserverEntry' in window
&& 'intersectionRatio' in window.IntersectionObserverEntry.prototype;
}
}
}
75 changes: 70 additions & 5 deletions test/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ describe('Module - Core', () => {
Core.config(JSON.stringify(config));
await Core.renderAsyncFragment('test');
await Core.renderAsyncFragment('test');

expect(fetchStub.calledOnce).to.eq(true);
expect(fetchStub.getCall(0).lastArg.headers).to.haveOwnProperty("originalurl");
expect(stubAsyncRenderResponse.calledOnce).to.eq(true);
Expand Down Expand Up @@ -288,7 +288,71 @@ describe('Module - Core', () => {
Core.config(JSON.stringify(config));
await Core.renderAsyncFragment('test');
await Core.renderAsyncFragment('test');


expect(fetchStub.calledOnce).to.eq(true);
expect(fetchStub.getCall(0).lastArg.headers).to.haveOwnProperty("originalurl");
expect(stubAsyncRenderResponse.calledOnce).to.eq(true);
});

it('should render async fragment withoutPathname', async () => {
const source = "source/";
const assets = [
{
name: 'bundle1',
dependent: ['vendor1'],
preLoaded: false,
link: 'bundle1.js',
fragment: 'test',
loadMethod: RESOURCE_LOADING_TYPE.ON_PAGE_RENDER,
type: RESOURCE_TYPE.JS
}
] as IPageLibAsset[];
const dependencies = [
{
name: 'vendor1',
link: 'vendor1.js',
preLoaded: false
}
] as IPageLibDependency[];
const config = {
dependencies,
assets,
fragments: [{
name: 'test',
attributes: {
if: "false",
withoutPathname: "true"
},
chunked: true,
clientAsync: true,
clientAsyncForce: undefined,
onDemand: undefined,
criticalCss: undefined,
source,
asyncDecentralized: false
}],
page: 'page',
peers: []
} as IPageLibConfiguration;

const fragmentContainer = global.window.document.createElement('div');
fragmentContainer.setAttribute('puzzle-fragment', 'test');
global.window.document.body.appendChild(fragmentContainer);

const fetchStub = global.fetch as SinonStub;
const stubAsyncRenderResponse = sandbox.stub(Core as any, "asyncRenderResponse").resolves();

Core.config(JSON.stringify(config));
await Core.renderAsyncFragment('test');
await Core.renderAsyncFragment('test');

const fragmentRequestUrl = `${source}?__renderMode=stream&if=false&withoutPathname=true`;
expect(fetchStub.calledWith(fragmentRequestUrl, {
headers: {
originalurl: window.location.pathname
},
credentials: 'include'
})).to.eq(true);
expect(fetchStub.calledOnce).to.eq(true);
expect(fetchStub.getCall(0).lastArg.headers).to.haveOwnProperty("originalurl");
expect(stubAsyncRenderResponse.calledOnce).to.eq(true);
Expand All @@ -307,7 +371,7 @@ describe('Module - Core', () => {

Core.config(JSON.stringify(config));
const result = Core.renderAsyncFragment('test');

expect(result).to.be.a('promise');
});

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

Core.config(JSON.stringify(config));
const result = Core.renderAsyncFragment('test');

expect(result).to.be.a('promise');
});
});

});

0 comments on commit 9bbbb7b

Please sign in to comment.