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: React Gen1 SDK: Symbols rendering #3739

Merged
merged 6 commits into from
Nov 17, 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
5 changes: 5 additions & 0 deletions .changeset/cool-pans-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/react': major
---

Breaking Change: Use `/query` instead of `/content` for API calls. This change fixes a symbol rendering issue introduced in https://github.com/BuilderIO/builder/pull/3681, which was included in the 5.0.0 release.
5 changes: 5 additions & 0 deletions .changeset/mild-pans-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/sdk': major
---

Breaking Change: Use `/query` instead of `/content` for API calls. This change fixes a symbol rendering issue introduced in https://github.com/BuilderIO/builder/pull/3681, which was included in the 4.0.0 release.
159 changes: 156 additions & 3 deletions packages/core/src/builder.class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ describe('flushGetContentQueue', () => {
);
});

test("hits content url when format is 'html'", async () => {
test("hits query url when apiEndpoint is undefined and format is 'html'", async () => {
const expectedFormat = 'html';

const result = await builder['flushGetContentQueue'](true, [
Expand All @@ -516,6 +516,35 @@ describe('flushGetContentQueue', () => {
userAttributes: { respectScheduling: true },
omit: OMIT,
fields: 'data',
},
]);

const observerNextMock = builder.observersByKey[MODEL]?.next as jest.Mock;

expect(observerNextMock).toBeCalledTimes(1);
expect(observerNextMock.mock.calls[0][0][0]).toStrictEqual({
...codegenOrQueryApiResult[MODEL][0],
variationId: expect.any(String),
});
expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/query/${API_KEY}/${MODEL}?omit=${OMIT}&apiKey=${API_KEY}&fields=data&format=${expectedFormat}&userAttributes=%7B%22respectScheduling%22%3Atrue%7D&options.${MODEL}.model=%22${MODEL}%22`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});

test("hits content url when apiEndpoint is 'content' and format is 'html'", async () => {
const expectedFormat = 'html';

const result = await builder['flushGetContentQueue'](true, [
{
apiEndpoint: 'content',
model: MODEL,
format: expectedFormat,
key: MODEL,
userAttributes: { respectScheduling: true },
omit: OMIT,
fields: 'data',
limit: 10,
},
]);
Expand All @@ -542,7 +571,7 @@ describe('flushGetContentQueue', () => {
);
});

test("hits content url when format is 'amp'", async () => {
test("hits query url when apiEndpoint is undefined and format is 'amp'", async () => {
const expectedFormat = 'amp';

const result = await builder['flushGetContentQueue'](true, [
Expand All @@ -553,6 +582,35 @@ describe('flushGetContentQueue', () => {
userAttributes: { respectScheduling: true },
omit: OMIT,
fields: 'data',
},
]);

const observerNextMock = builder.observersByKey[MODEL]?.next as jest.Mock;

expect(observerNextMock).toBeCalledTimes(1);
expect(observerNextMock.mock.calls[0][0][0]).toStrictEqual({
...codegenOrQueryApiResult[MODEL][0],
variationId: expect.any(String),
});
expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/query/${API_KEY}/${MODEL}?omit=${OMIT}&apiKey=${API_KEY}&fields=data&format=${expectedFormat}&userAttributes=%7B%22respectScheduling%22%3Atrue%7D&options.${MODEL}.model=%22${MODEL}%22`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});

test("hits content url when apiEndpoint is 'content' and format is 'amp'", async () => {
const expectedFormat = 'amp';

const result = await builder['flushGetContentQueue'](true, [
{
apiEndpoint: 'content',
model: MODEL,
format: expectedFormat,
key: MODEL,
userAttributes: { respectScheduling: true },
omit: OMIT,
fields: 'data',
limit: 10,
},
]);
Expand All @@ -579,7 +637,7 @@ describe('flushGetContentQueue', () => {
);
});

test("hits content url when format is 'email'", async () => {
test("hits query url when apiEndpoint is undefined and format is 'email'", async () => {
const expectedFormat = 'email';

const result = await builder['flushGetContentQueue'](true, [
Expand All @@ -590,6 +648,63 @@ describe('flushGetContentQueue', () => {
userAttributes: { respectScheduling: true },
omit: OMIT,
fields: 'data',
},
]);

const observerNextMock = builder.observersByKey[MODEL]?.next as jest.Mock;

expect(observerNextMock).toBeCalledTimes(1);
expect(observerNextMock.mock.calls[0][0][0]).toStrictEqual({
...codegenOrQueryApiResult[MODEL][0],
variationId: expect.any(String),
});
expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/query/${API_KEY}/${MODEL}?omit=${OMIT}&apiKey=${API_KEY}&fields=data&format=${expectedFormat}&userAttributes=%7B%22respectScheduling%22%3Atrue%7D&options.${MODEL}.model=%22${MODEL}%22`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});

test("hits query url when apiEndpoint is undefined and format is 'email' and url is passed instead of userAttributes", async () => {
const expectedFormat = 'email';

const result = await builder['flushGetContentQueue'](true, [
{
model: MODEL,
format: expectedFormat,
key: MODEL,
url: '/test-page',
omit: OMIT,
fields: 'data',
},
]);

const observerNextMock = builder.observersByKey[MODEL]?.next as jest.Mock;

expect(observerNextMock).toBeCalledTimes(1);
expect(observerNextMock.mock.calls[0][0][0]).toStrictEqual({
...codegenOrQueryApiResult[MODEL][0],
variationId: expect.any(String),
});
expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/query/${API_KEY}/${MODEL}?omit=${OMIT}&apiKey=${API_KEY}&fields=data&format=${expectedFormat}&userAttributes=%7B%22urlPath%22%3A%22%2Ftest-page%22%2C%22host%22%3A%22localhost%22%2C%22device%22%3A%22desktop%22%7D&options.${MODEL}.model=%22${MODEL}%22`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});

test("hits content url when apiEndpoint is 'content' and format is 'email'", async () => {
const expectedFormat = 'email';

const result = await builder['flushGetContentQueue'](true, [
{
apiEndpoint: 'content',
model: MODEL,
format: expectedFormat,
key: MODEL,
userAttributes: { respectScheduling: true },
omit: OMIT,
fields: 'data',
limit: 10,
},
]);
Expand All @@ -615,4 +730,42 @@ describe('flushGetContentQueue', () => {
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});

test("hits content url when apiEndpoint is 'content' and format is 'email' and url is passed instead of userAttributes", async () => {
const expectedFormat = 'email';

const result = await builder['flushGetContentQueue'](true, [
{
apiEndpoint: 'content',
model: MODEL,
format: expectedFormat,
key: MODEL,
url: '/test-page',
omit: OMIT,
fields: 'data',
limit: 10,
},
]);

const observerNextMock = builder.observersByKey[MODEL]?.next as jest.Mock;

expect(observerNextMock).toBeCalledTimes(1);
expect(observerNextMock.mock.calls[0][0][0]).toStrictEqual({
...contentApiResult.results[0],
variationId: expect.any(String),
});
expect(observerNextMock.mock.calls[0][0][1]).toStrictEqual({
...contentApiResult.results[1],
});
expect(observerNextMock.mock.calls[0][0][2]).toStrictEqual({
...contentApiResult.results[2],
variationId: expect.any(String),
});

expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/content/${MODEL}?omit=data.blocks&apiKey=${API_KEY}&fields=data&format=${expectedFormat}&userAttributes=%7B%22urlPath%22%3A%22%2Ftest-page%22%2C%22host%22%3A%22localhost%22%2C%22device%22%3A%22desktop%22%7D&limit=10&model=%22${MODEL}%22&enrich=true`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});
});
21 changes: 15 additions & 6 deletions packages/core/src/builder.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ type AllowEnrich =
| { apiVersion?: never; enrich?: boolean };

export type GetContentOptions = AllowEnrich & {
/**
* Dictates which API endpoint is used when fetching content. Allows `'content'` and `'query'`.
* Defaults to `'query'`.
*/
apiEndpoint?: 'content' | 'query';

/**
* Optional fetch options to be passed as the second argument to the `fetch` function.
*/
Expand Down Expand Up @@ -2454,6 +2460,8 @@ export class Builder {

const queue = useQueue || (usePastQueue ? this.priorContentQueue : this.getContentQueue) || [];

const apiEndpoint = queue[0].apiEndpoint || 'query';

// TODO: do this on every request send?
this.getOverridesFromQueryString();

Expand Down Expand Up @@ -2547,9 +2555,6 @@ export class Builder {
}
}

const isApiCallForCodegen =
queue[0].options?.format === 'solid' || queue[0].options?.format === 'react';

for (const options of queue) {
const format = options.format;

Expand Down Expand Up @@ -2588,7 +2593,7 @@ export class Builder {
for (const key of properties) {
const value = options[key];
if (value !== undefined) {
if (isApiCallForCodegen) {
if (apiEndpoint === 'query') {
queryParams.options = queryParams.options || {};
queryParams.options[options.key!] = queryParams.options[options.key!] || {};
queryParams.options[options.key!][key] = JSON.stringify(value);
Expand All @@ -2614,8 +2619,10 @@ export class Builder {
}

const format = queryParams.format;
const isApiCallForCodegen = format === 'solid' || format === 'react';
const isApiCallForCodegenOrQuery = isApiCallForCodegen || apiEndpoint === 'query';

if (!isApiCallForCodegen) {
if (apiEndpoint === 'content') {
queryParams.enrich = true;
if (queue[0].query) {
const flattened = this.flattenMongoQuery({ query: queue[0].query });
Expand All @@ -2639,6 +2646,8 @@ export class Builder {
let url;
if (isApiCallForCodegen) {
url = `${host}/api/v1/codegen/${this.apiKey}/${keyNames}`;
} else if (apiEndpoint === 'query') {
url = `${host}/api/v3/query/${this.apiKey}/${keyNames}`;
Comment on lines +2649 to +2650
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this PR re-introduces apiEndpoint prop, with a default of 'query'. This fixes the rendering issue for symbols

} else {
url = `${host}/api/v3/content/${queue[0].model}`;
}
Expand All @@ -2664,7 +2673,7 @@ export class Builder {
if (!observer) {
return;
}
const data = isApiCallForCodegen ? result[keyName] : result.results;
const data = isApiCallForCodegenOrQuery ? result[keyName] : result.results;
const sorted = data; // sortBy(data, item => item.priority);
if (data) {
const testModifiedResults = Builder.isServer
Expand Down
4 changes: 2 additions & 2 deletions packages/sdks-tests/src/e2e-tests/hit-content-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test.describe('Get Content', () => {
});

await page.goto('/get-content', { waitUntil: 'networkidle' });
expect(contentApiInvocations).toBeGreaterThan(0);
expect(contentApiInvocations).toBe(1);

// Check for new SDK headers
expect(headers?.['x-builder-sdk']).toBe(mapSdkName(sdk));
Expand All @@ -30,7 +30,7 @@ test.describe('Get Content', () => {
test('passes fetch options', async ({ page, packageName }) => {
test.skip(packageName !== 'gen1-next');

const urlMatch = /https:\/\/cdn\.builder\.io\/api\/v3\/content/;
const urlMatch = /https:\/\/cdn\.builder\.io\/api\/v3\/query/;
const responsePromise = page.waitForResponse(urlMatch);

await page.goto('/with-fetch-options', { waitUntil: 'networkidle' });
Expand Down
23 changes: 23 additions & 0 deletions packages/sdks-tests/src/e2e-tests/hit-query-api.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { expect } from '@playwright/test';
import { excludeGen1, test } from '../helpers/index.js';

test.describe('Get Query', () => {
test('call query API only once - in page', async ({ page, sdk }) => {
test.skip(!excludeGen1(sdk));

const urlMatch = /https:\/\/cdn\.builder\.io\/api\/v3\/query/;

let queryApiInvocations = 0;

await page.route(urlMatch, route => {
queryApiInvocations++;
return route.fulfill({
status: 200,
json: {},
});
});

await page.goto('/get-query', { waitUntil: 'networkidle' });
expect(queryApiInvocations).toBe(1);
});
});
14 changes: 11 additions & 3 deletions packages/sdks-tests/src/e2e-tests/slot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test.describe('Slot', () => {
await expect(page.locator('text=This is called recursion!')).toBeVisible();
});

test('slot should render with symbol (without content)', async ({ page, packageName }) => {
test('slot should render with symbol (without content)', async ({ page, packageName, sdk }) => {
// gen1-remix and gen1-next skipped because React.useContext is not recognized
// ssr packages skipped because it fetches the slot content from the server
test.fail(
Expand All @@ -40,15 +40,23 @@ test.describe('Slot', () => {

let x = 0;

const urlMatch = /https:\/\/cdn\.builder\.io\/api\/v3\/content\/symbol\.*/;
const urlMatch =
sdk === 'oldReact'
? 'https://cdn.builder.io/api/v3/query/abcd/symbol*'
: /https:\/\/cdn\.builder\.io\/api\/v3\/content\/symbol\.*/;

await page.route(urlMatch, route => {
x++;

const url = new URL(route.request().url());

const keyName =
sdk === 'oldReact' ? decodeURIComponent(url.pathname).split('/').reverse()[0] : 'results';

return route.fulfill({
status: 200,
json: {
results: [x === 0 ? FIRST_SYMBOL_CONTENT : SECOND_SYMBOL_CONTENT],
[keyName]: [x === 0 ? FIRST_SYMBOL_CONTENT : SECOND_SYMBOL_CONTENT],
},
});
});
Expand Down
Loading
Loading