Skip to content

Commit abefe61

Browse files
committed
Add try/catch block to avoid crash on cookie read (SSG failure at build step for unknown reason)
1 parent f747475 commit abefe61

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/utils/UniversalCookiesManager.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,28 @@ export default class UniversalCookiesManager {
5454
* @param browserOptions
5555
*/
5656
replaceUserData(newUserData: UserSemiPersistentSession, serverOptions = this.defaultServerOptions, browserOptions: CookieAttributes = this.defaultBrowserOptions): void {
57-
if (isBrowser()) {
58-
// XXX By default, "js-cookies" apply a "percent encoding" when writing data, which isn't compatible with the "cookies" lib
59-
// We therefore override this behaviour because we need to write proper JSON
60-
// See https://github.com/js-cookie/js-cookie#encoding
61-
const browserCookies = BrowserCookies.withConverter({
62-
write: function (value: string, name: string) {
63-
return value;
64-
},
65-
});
66-
browserCookies.set(USER_LS_KEY, JSON.stringify(newUserData), browserOptions);
67-
} else {
68-
const serverCookies = new ServerCookies(this.req, this.res);
57+
try {
58+
if (isBrowser()) {
59+
// XXX By default, "js-cookies" apply a "percent encoding" when writing data, which isn't compatible with the "cookies" lib
60+
// We therefore override this behaviour because we need to write proper JSON
61+
// See https://github.com/js-cookie/js-cookie#encoding
62+
const browserCookies = BrowserCookies.withConverter({
63+
write: function (value: string, name: string) {
64+
return value;
65+
},
66+
});
67+
browserCookies.set(USER_LS_KEY, JSON.stringify(newUserData), browserOptions);
68+
} else {
69+
const serverCookies = new ServerCookies(this.req, this.res);
6970

70-
// If running on the server side but req or res aren't set, then we don't do anything
71-
// It's likely because we're calling this code from a view (that doesn't belong to getInitialProps and doesn't have access to req/res even though if it's running on the server)
72-
if (this.req && this.res) {
73-
serverCookies.set(USER_LS_KEY, JSON.stringify(newUserData), serverOptions);
71+
// If running on the server side but req or res aren't set, then we don't do anything
72+
// It's likely because we're calling this code from a view (that doesn't belong to getInitialProps and doesn't have access to req/res even though if it's running on the server)
73+
if (this.req && this.res) {
74+
serverCookies.set(USER_LS_KEY, JSON.stringify(newUserData), serverOptions);
75+
}
7476
}
77+
} catch (e) {
78+
Sentry.captureException(e);
7579
}
7680
}
7781

0 commit comments

Comments
 (0)