Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Named export DocSearch not found" error in Remix framework #2259

Open
pawelgalazka opened this issue Jun 17, 2024 · 2 comments
Open

"Named export DocSearch not found" error in Remix framework #2259

pawelgalazka opened this issue Jun 17, 2024 · 2 comments
Labels
Needs investigation Investigation is planned but not started yet

Comments

@pawelgalazka
Copy link

Description

When importing DocSearch React component in Remix framework web app, following error is thrown when running application:

[vite] Internal server error: [vite] Named export 'DocSearch' not found. The requested module '@docsearch/react' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@docsearch/react';
const {DocSearch} = pkg;

Steps to reproduce

  1. Create Remix app through npx create-remix@latest
  2. Add DocSearch import to app/routes/_index.tsx:
import { DocSearch } from '@docsearch/react';

import '@docsearch/css';
  1. Run application in dev mode: npm run dev
  2. Visit local page: http://localhost:5173/

Expected behavior

Error:

Internal server error: [vite] Named export 'DocSearch' not found.

should not be produced when running web app in Remix framework. Instead DocSearch search component should be rendered.

Environment

  • OS: Mac
  • Browser: Chrome
  • DocSearch version: 3.6.0
@randombeeper
Copy link
Collaborator

randombeeper commented Jul 10, 2024

Hello, @pawelgalazka sorry for the delayed response. Did you resolve this? I don't know off the top of my head so wondering if you managed to figure it out before investing time on it.

@pawelgalazka
Copy link
Author

pawelgalazka commented Jul 11, 2024

@randombeeper I did manage to resolve this, however my solution is more of a workaround:

  1. In vite.config.ts add:
ssr: {
  noExternal: ["@docsearch/react"],
},
  1. Then use this wrapper component for @docsearch/react:
import { Suspense, lazy, useEffect, useState } from "react"
import type { DocSearchProps } from "@docsearch/react"
import "@docsearch/css"

let hydrating = true

export function useHydrated() {
  const [hydrated, setHydrated] = useState(() => !hydrating)
  useEffect(() => {
    hydrating = false
    setHydrated(true)
  }, [])
  return hydrated
}

const OriginalDocSearch = lazy(() =>
  import("@docsearch/react").then((module) => ({
    default: module.DocSearch,
  })),
)

export function DocSearch(docSearchProps: DocSearchProps) {
  const hydrated = useHydrated()

  if (!hydrated) {
    return <div className="h-9" />
  }

  return (
    <Suspense fallback={<div className="h-9" />}>
      <div className="-ml-4 flex flex-col">
        <OriginalDocSearch {...docSearchProps} />
      </div>
    </Suspense>
  )
}

which excludes @docsearch/react from being server side rendered.

Ideally I would expect to import and use original DocSearch React component as is, in my server side rendered component without any issues.

@randombeeper randombeeper added the Needs investigation Investigation is planned but not started yet label Jul 11, 2024
acusti added a commit to acusti/superflare that referenced this issue Jul 24, 2024
acusti added a commit to acusti/superflare that referenced this issue Jul 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs investigation Investigation is planned but not started yet
Projects
None yet
Development

No branches or pull requests

2 participants