Skip to content

Commit

Permalink
SSR Support (#115)
Browse files Browse the repository at this point in the history
Add SSR support by adding "type": "module" and useIsomorphicLayoutEffect

By setting "type": "module", node treats the javascript files within the package as ES modules. Since some of the config files are CJS, we need to indicate that by updating the file extension from '.js' to '.cjs' or else node 16+ will throw an error

J=none
TEST=manual

Connect this to the [ SSR Support ](yext/search-ui-react#125) branch in the component library and smoke test with the Yext Sites starter
  • Loading branch information
cea2aj authored Apr 22, 2022
1 parent c51aea7 commit 25d21c2
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 18 deletions.
File renamed without changes.
30 changes: 30 additions & 0 deletions THIRD-PARTY-NOTICES
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,36 @@ This package contains the following license and notice below:

-----------

The following NPM package may be included in this product:

- [email protected]

This package contains the following license and notice below:

MIT License

Copyright (c) Mateusz Burzyński

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

-----------

The following NPM package may be included in this product:

- [email protected]
Expand Down
File renamed without changes.
40 changes: 26 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "@yext/answers-headless-react",
"version": "1.1.0-beta.11",
"version": "1.1.0-beta.12",
"types": "./lib/src/index.d.ts",
"main": "./lib/src/index.js",
"module": "./lib/src/index.js",
"type": "module",
"files": [
"lib",
"src"
Expand All @@ -16,7 +17,8 @@
"generate-notices": "generate-license-file --input package.json --output THIRD-PARTY-NOTICES --overwrite"
},
"dependencies": {
"@yext/answers-headless": "^1.1.0-beta.12"
"@yext/answers-headless": "^1.1.0-beta.12",
"use-isomorphic-layout-effect": "^1.1.2"
},
"devDependencies": {
"@babel/preset-env": "^7.16.5",
Expand Down
3 changes: 2 additions & 1 deletion src/useAnswersState.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useContext, useLayoutEffect, useRef, useState } from 'react';
import { useContext, useRef, useState } from 'react';
import { State } from '@yext/answers-headless';
import { AnswersHeadlessContext } from './AnswersHeadlessContext';
import useLayoutEffect from 'use-isomorphic-layout-effect';

export type StateSelector<T> = (s: State) => T;

Expand Down
6 changes: 5 additions & 1 deletion src/utils/acquireSessionId.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { v4 as uuidv4 } from 'uuid';
/**
* Retrieves session id from local storage, or generates a new uuid to use as session id.
* The new id is then stored in local storage.
* The new id is then stored in local storage. Returns null if the sessionStorage API is
* unavailable. (e.g. The function is running on the server for SSR).
*/
export default function acquireSessionId(): string | null {
if (typeof(window) === 'undefined') {
return null;
}
try {
let sessionId = window.sessionStorage.getItem('sessionId');
if (!sessionId) {
Expand Down

0 comments on commit 25d21c2

Please sign in to comment.