Skip to content

Commit 4b6315a

Browse files
Merge pull request #83 from Shopify/profile-with-correct-render-backend
Fix profile error due to incorrect detection of render backend
2 parents 5610ce3 + 300e951 commit 4b6315a

7 files changed

+83
-68
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Change Log
22

3+
## v2.0.1 (Jan 26, 2021)
4+
* [#83](https://github.com/Shopify/shopify-theme-inspector/pull/83) Fix profiling error due to incorrect detection of render backend
5+
36
## v2.0.0 (Jan 4, 2021)
47
* [#77](https://github.com/Shopify/shopify-theme-inspector/pull/77) Update data format with change update
58

src/background.ts

+37-43
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ chrome.runtime.onMessage.addListener((event, sender) => {
8989
chrome.runtime.onMessage.addListener((event, sender) => {
9090
if (sender.tab && sender.tab.id && event.type === 'detect-shopify') {
9191
setIconAndPopup(event.hasDetectedShopify, sender.tab.id);
92-
renderBackend = event.isCore
93-
? RenderBackend.Core
94-
: RenderBackend.StorefrontRenderer;
9592
}
9693
});
9794

@@ -117,39 +114,45 @@ chrome.runtime.onMessage.addListener(({type, origin}, _, sendResponse) => {
117114

118115
// Listen for 'request-core-access-token' event and respond to the messenger
119116
// with a valid access token. This may trigger a login popup window if needed.
120-
chrome.runtime.onMessage.addListener(({type, origin}, _, sendResponse) => {
121-
if (type !== 'request-core-access-token') {
122-
return false;
123-
}
117+
chrome.runtime.onMessage.addListener(
118+
({type, origin, isCore}, _, sendResponse) => {
119+
if (type !== 'request-core-access-token') {
120+
return false;
121+
}
124122

125-
const params = [
126-
[
127-
'scope',
128-
`${shopifyEmployee === true ? 'employee' : ''} ${
129-
env.DEVTOOLS_SCOPE[renderBackend]
130-
} ${COLLABORATORS_SCOPE}`,
131-
],
132-
];
133-
134-
// SFR does not need a destination.
135-
const destination =
136-
renderBackend === RenderBackend.Core ? `${origin}/admin` : '';
137-
138-
const oauth = getOauth2Client(origin);
139-
140-
getSubjectId(oauth, origin)
141-
.then(subjectId => {
142-
return oauth.getSubjectAccessToken(destination, subjectId, params);
143-
})
144-
.then(token => {
145-
sendResponse({token});
146-
})
147-
.catch(error => {
148-
sendResponse({error});
149-
});
123+
renderBackend = isCore
124+
? RenderBackend.Core
125+
: RenderBackend.StorefrontRenderer;
150126

151-
return true;
152-
});
127+
const params = [
128+
[
129+
'scope',
130+
`${shopifyEmployee === true ? 'employee' : ''} ${
131+
env.DEVTOOLS_SCOPE[renderBackend]
132+
} ${COLLABORATORS_SCOPE}`,
133+
],
134+
];
135+
136+
// SFR does not need a destination.
137+
const destination =
138+
renderBackend === RenderBackend.Core ? `${origin}/admin` : '';
139+
140+
const oauth = getOauth2Client(origin);
141+
142+
getSubjectId(oauth, origin)
143+
.then(subjectId => {
144+
return oauth.getSubjectAccessToken(destination, subjectId, params);
145+
})
146+
.then(token => {
147+
sendResponse({token});
148+
})
149+
.catch(error => {
150+
sendResponse({error});
151+
});
152+
153+
return true;
154+
},
155+
);
153156

154157
// Listen for the 'request-user-info' event and respond to the messenger
155158
// with a the given_name of the currently logged in user.
@@ -185,12 +188,3 @@ chrome.runtime.onMessage.addListener(({type, origin}, _, sendResponse) => {
185188

186189
return true;
187190
});
188-
189-
chrome.runtime.onMessage.addListener(({type}, _, sendResponse) => {
190-
if (type !== 'request-rendering-backend') return false;
191-
192-
const name = renderBackend.toString();
193-
sendResponse({name});
194-
195-
return true;
196-
});

src/detectShopify.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,21 @@
44
// back-end that rendered this page.
55
// Both are generated by {{content_for_header}}.
66
let hasDetectedShopify = false;
7-
let isCore = false;
87

98
const scripts = document.querySelectorAll('script');
109
for (let i = 0; i < scripts.length; i++) {
11-
// Short-circuit if we have found everything we need
12-
if (isCore && hasDetectedShopify) break;
13-
1410
const content = scripts[i].textContent;
1511
if (typeof content === 'string') {
1612
if (/Shopify\.shop\s*=/.test(content)) {
1713
hasDetectedShopify = true;
18-
}
19-
if (/BOOMR\.application\s*=\s*"core"/.test(content)) {
20-
isCore = true;
14+
break;
2115
}
2216
}
2317
}
2418

2519
chrome.runtime.sendMessage({
2620
type: 'detect-shopify',
2721
hasDetectedShopify,
28-
isCore,
2922
});
3023

3124
if (document.location.search.includes('shopify_employee')) {

src/devtools.ts

+28-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {escape} from 'lodash';
2+
import {RenderBackend} from './env';
23
import Toolbar from './components/toolbar';
34
import LiquidFlamegraph from './components/liquid-flamegraph';
45
import {
@@ -91,15 +92,28 @@ function getInspectedWindowURL(): Promise<URL> {
9192
});
9293
}
9394

94-
function getRenderingBackend(): Promise<string> {
95-
return new Promise((resolve, reject) => {
96-
chrome.runtime.sendMessage(
97-
{type: 'request-rendering-backend'},
98-
({name, error}) => {
99-
if (error) {
100-
return reject(error);
95+
function determineRenderBackend(): Promise<boolean> {
96+
return new Promise(resolve => {
97+
chrome.devtools.inspectedWindow.eval(
98+
`
99+
function determineRenderBackend() {
100+
const scripts = document.querySelectorAll('script');
101+
let isCore = false;
102+
for (let i = 0; i < scripts.length; i++) {
103+
const content = scripts[i].textContent;
104+
if (typeof content === 'string') {
105+
if (/BOOMR\\.application\\s*=\\s*"core"/.test(content)) {
106+
isCore = true;
107+
break;
108+
}
109+
}
101110
}
102-
return resolve(name);
111+
return isCore
112+
}
113+
determineRenderBackend()
114+
`,
115+
function(isCore: boolean) {
116+
resolve(isCore);
103117
},
104118
);
105119
});
@@ -115,18 +129,21 @@ async function refreshPanel() {
115129

116130
let profile: FormattedProfileData;
117131
const url = await getInspectedWindowURL();
132+
const isCore = await determineRenderBackend();
133+
134+
const renderingBackend = isCore
135+
? RenderBackend.Core
136+
: RenderBackend.StorefrontRenderer;
118137

119138
try {
120-
profile = await getProfileData(url);
139+
profile = await getProfileData(url, isCore);
121140

122141
liquidFlamegraph = new LiquidFlamegraph(
123142
document.querySelector(selectors.flamegraphContainer),
124143
profile,
125144
url,
126145
);
127146

128-
const renderingBackend = await getRenderingBackend();
129-
130147
// All events happening here are synchronous. The set timeout is for UI
131148
// purposes so that timing information gets displayed after the flamegraph is shown.
132149
setTimeout(function() {

src/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Shopify Theme Inspector for Chrome",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Profile and debug Liquid template on your Shopify store",
55
"devtools_page": "devtools.html",
66
"permissions": ["storage", "identity", "activeTab"],

src/utils/getProfileData.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import nullthrows from 'nullthrows';
22
import {SubjectAccessToken} from 'types';
33

4-
export async function getProfileData(url: URL): Promise<FormattedProfileData> {
4+
export async function getProfileData(
5+
url: URL,
6+
isCore: boolean,
7+
): Promise<FormattedProfileData> {
58
const parser = new DOMParser();
69

710
const fetchOptions = {} as any;
8-
const {accessToken} = await requestAccessToken(url);
11+
const {accessToken} = await requestAccessToken(url, isCore);
912
fetchOptions.headers = {Authorization: `Bearer ${accessToken}`};
1013

1114
url.searchParams.set('profile_liquid', 'true');
@@ -31,10 +34,13 @@ function noProfileFound(document: HTMLDocument) {
3134
return document.querySelector('#liquidProfileData') === null;
3235
}
3336

34-
function requestAccessToken({origin}: URL): Promise<SubjectAccessToken> {
37+
function requestAccessToken(
38+
{origin}: URL,
39+
isCore: boolean,
40+
): Promise<SubjectAccessToken> {
3541
return new Promise((resolve, reject) => {
3642
return chrome.runtime.sendMessage(
37-
{type: 'request-core-access-token', origin},
43+
{type: 'request-core-access-token', origin, isCore},
3844
({token, error}) => {
3945
if (error) {
4046
return reject(error);

test/test-helpers.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ export function setDevtoolsEval(page: any) {
2424
window.chrome.devtools.inspectedWindow.eval = function(value, cb){
2525
if (value === "typeof window.Shopify === 'object'") {
2626
return cb(true)
27-
} else if (/Shopify\\.shop/.test(value)) {
27+
} else if (/Shopify\\.shop/.test(value)) {
2828
return cb('shop1.myshopify.io')
29+
} else if (/determineRenderBackend/.test(value)) {
30+
return cb(true)
2931
}
3032
};
3133
window.chrome.devtools.panels.create = () => {};

0 commit comments

Comments
 (0)