extended-component-library icon indicating copy to clipboard operation
extended-component-library copied to clipboard

HTMLDivElement is not defined

Open FrancisBaah opened this issue 2 years ago • 8 comments

import { APILoader } from '@googlemaps/extended-component-library/react'; <APILoader apiKey={mykey} />

The code works fine on localhost, but give "HTMLDivElement is not defined" error on deploy.

FrancisBaah avatar Nov 04 '23 15:11 FrancisBaah

If you would like to upvote the priority of this issue, please comment below or react on the original post above with :+1: so we can see what is popular when we triage.

@FrancisBaah Thank you for opening this issue. 🙏 Please check out these other resources that might help you get to a resolution in the meantime:

This is an automated message, feel free to ignore.

wangela avatar Nov 04 '23 15:11 wangela

@FrancisBaah Could you provide more info/steps on how you are deploying the app, e.g. are you using one of the approaches described here? This will help us reproduce the issue. Thanks!

leafsy avatar Nov 07 '23 18:11 leafsy

Error occurs in Next.js (Pages router) during server-side rendering:

 ⨯ ReferenceError: HTMLDivElement is not defined
    at file:///.../node_modules/@googlemaps/extended-component-library/lib/overlay_layout/overlay_layout.js:182:31
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:336:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:429:15) {
  page: '/'
}

When importing any React-component, e.g.:

import { PlacePicker } from "@googlemaps/extended-component-library/react";

Originates from overlay_layout.js:182:

180: __decorate([
181:     query('.main-container'),
182:     __metadata("design:type", HTMLDivElement)
183: ], OverlayLayout.prototype, "mainContainer", void 0);

Other similar lib/<component>/<component>.js files are probably susceptible to this too.

codeclown avatar Jun 17 '24 06:06 codeclown

I'm having the same problem using Next.js

ImperadorSid avatar Jul 04 '24 16:07 ImperadorSid

Hi @codeclown @ImperadorSid, unfortunately Web Components in general don't work well with server-side rendering, though there're workarounds in the case of Next.js. Could you check if the dynamic loading technique used in this sample project helps? See docs for more info. Another option you may explore is @lit-labs/nextjs, but note that Lit SSR is under active development and has some limitations.

leafsy avatar Jul 08 '24 16:07 leafsy

You need to do this

const DynamicCollegeFinderPage = dynamic( () => import('@/components/custom/map/CollegeFinderPage ').then((mod) => mod.CollegeFinderPage), { ssr: false, loading: () => <p>Loading...</p>, } )

jt6677 avatar Aug 06 '24 08:08 jt6677

Dynamic loading doesn't seem to fully resolve the issue, because the error mentions overlay_layout.js, which isn't (or can't?) directly imported.

morgler avatar Aug 18 '24 19:08 morgler

Dynamic loading solves the issue for me in NextJs. A snippet:

'use client';
import React from 'react';
import dynamic from 'next/dynamic';
///.......
export const BaseMap = ({
  defaultValues,
  actions,
  mapOrigin,
  permits,
  children,
}: React.PropsWithChildren<Props>) => {
  const PlacePicker = dynamic(
    () =>
      import('@googlemaps/extended-component-library/react').then(
        (mod) => mod.PlacePicker,
      ),
    { ssr: false },
  );
  
///.......
};  

HMubaireek avatar Sep 05 '24 08:09 HMubaireek