-
Notifications
You must be signed in to change notification settings - Fork 176
Description
Target Use Case
We are using @vis.gl/react-google-maps
in a project that renders pages using server-side rendering, and then hydrates them on the client.
However, the current package assumes a browser environment by default — for example, components like AdvancedMarker
use ReactDOM.createPortal
and interact with the DOM, which causes SSR crashes when the page is pre-rendered on the server.
We are currently forced to wrap these components in client-only guards like:
if (typeof window !== 'undefined') {
return <AdvancedMarker ... />;
}
const [isClient, setIsClient] = useState(false);
useEffect(() => {
setIsClient(true);
}, []);
return isClient ? <AdvancedMarker ... /> : null;
It would be helpful if the library could offer a built-in way to make components like AdvancedMarker
SSR-safe or optionally lazy-loadable so that SSR apps don’t crash or require workarounds.
Adding SSR support (or safe lazy loading) would benefit anyone using React frameworks like Next.js, Remix, or custom SSR setups that render Google Maps on the server and hydrate them client-side.
This is especially important for:
- SEO-sensitive apps
- Performance-focused web apps
- Sites with server-rendered landing pages that contain maps
It would also reduce the need for wrapping AdvancedMarker and other DOM-dependent components in custom client-only guards, simplifying code and avoiding errors during hydration.
Proposal
- Provide a
ssr: false
orclientOnly: true
prop on components likeAdvancedMarker
orAPIProvider
that tells them to skip rendering during SSR. - Internally guard browser-only operations in DOM-based components like
AdvancedMarker
by checkingtypeof window !== 'undefined'
before callingReactDOM.createPortal
. - Document a recommended pattern or wrapper component for SSR users, such as a ClientOnly wrapper like:
function ClientOnly({ children }) {
const [isClient, setIsClient] = useState(false);
useEffect(() => setIsClient(true), []);
return isClient ? children : null;
}
- Optionally provide a utility from the package itself:
import { ClientOnly } from '@vis.gl/react-google-maps';
// or
<AdvancedMarker ssr={false} ... />
Even a small guard inside AdvancedMarker
(as well as MapControl
) would prevent runtime crashes and improve compatibility across frameworks.