Skip to content

Commit

Permalink
fix: ssr invalid url
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Oct 18, 2020
1 parent e7a36ab commit aa7b5a2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
19 changes: 12 additions & 7 deletions ui/blocks/src/PageContainer/PageContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,18 @@ export const PageContainer: FC<PageContainerProps> = forwardRef(
const parseURLHash = () => {
try {
const url = getURL();
const scrollId = url.hash ? url.hash.substring(1) : undefined;
if (scrollId) {
const element = document.getElementById(scrollId);
if (element) {
setTimeout(() => {
element.scrollIntoView({ behavior: 'smooth', block: 'start' });
}, 100);
if (url) {
const scrollId = url.hash ? url.hash.substring(1) : undefined;
if (scrollId) {
const element = document.getElementById(scrollId);
if (element) {
setTimeout(() => {
element.scrollIntoView({
behavior: 'smooth',
block: 'start',
});
}, 100);
}
}
}
} catch (err) {}
Expand Down
24 changes: 13 additions & 11 deletions ui/blocks/src/PropsTable/controlsActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,19 @@ export const useControlsActions = (props: UseControlsActionsProps) => {
onClick: () => {
if (controls) {
const url = getURL();
const parsedParams = queryString.parse(url.search);
const values = getControlValues(controls);
parsedParams.controls = JSON.stringify(values);
const strValues = queryString.stringify(parsedParams);
const copyURL = `${url.protocol}//${url.host}${url.pathname}${
strValues ? `?${strValues}` : ''
}${url.hash ? `#${url.hash}` : ''}`;
copy(copyURL);
if (typeof window !== 'undefined') {
setURLCopied(true);
window.setTimeout(() => setURLCopied(false), 1500);
if (url) {
const parsedParams = queryString.parse(url.search);
const values = getControlValues(controls);
parsedParams.controls = JSON.stringify(values);
const strValues = queryString.stringify(parsedParams);
const copyURL = `${url.protocol}//${url.host}${url.pathname}${
strValues ? `?${strValues}` : ''
}${url.hash ? `#${url.hash}` : ''}`;
copy(copyURL);
if (typeof window !== 'undefined') {
setURLCopied(true);
window.setTimeout(() => setURLCopied(false), 1500);
}
}
}
},
Expand Down
12 changes: 7 additions & 5 deletions ui/blocks/src/context/BlockContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ export const BlockContextProvider: React.FC<StateRootProps &
}) => {
const values = useMemo(() => {
const url = getURL();
const parsedParams = queryString.parse(url.search);
if (typeof parsedParams.controls === 'string') {
return typeof parsedParams.controls === 'string'
? JSON.parse(parsedParams.controls)
: parsedParams.controls;
if (url) {
const parsedParams = queryString.parse(url.search);
if (typeof parsedParams.controls === 'string') {
return typeof parsedParams.controls === 'string'
? JSON.parse(parsedParams.controls)
: parsedParams.controls;
}
}
return undefined;
}, []);
Expand Down
4 changes: 2 additions & 2 deletions ui/blocks/src/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export const getURL = () => {
? window.parent.location.href
: typeof document !== 'undefined'
? document.location.href
: '') || '';
return new URL(pageURL);
: undefined) || undefined;
return pageURL ? new URL(pageURL) : undefined;
};

0 comments on commit aa7b5a2

Please sign in to comment.