Skip to content

[labs/nextjs] Support for the app directory #3657

Closed

Description

@augustjk

Should this be an RFC?

  • This is not a substantial change

Which package is this a feature request for?

Other/unknown (please mention in description)

Description

Next.js v13 introduced the app directory, an alternative to the existing pages directory. https://nextjs.org/blog/next-13#new-app-directory-beta

This feature is considered beta and not recommended for production at this time.

The current implementation of the Lit SSR plugin for Next.js won't work for users using this as we're looking specifically for the pages directory for the entry points.

Supporting this may be as simple as adding to the test regex but will require testing.

Alternatives and Workarounds

.

Activity

  1. ericis commented on May 15, 2023

    @ericis

    @augustjk et al, this is no longer marked as experimental in the NextJS configuration when using "create-next-app".

    See the PR vercel/next.js#49241

    And, here is an example:
    https://github.com/vercel/next.js/blob/a7601213ab1bd2fd75e120907cd7c70278d7ebc7/packages/create-next-app/templates/app/js/next.config.js#L2

    /** @type {import('next').NextConfig} */
    const nextConfig = {}
    
    module.exports = nextConfig

    The "old" way used to look like:

    /** @type {import('next').NextConfig} */
    const nextConfig = {
      experimental: {
        appDir: true,
      },
    }
    
    module.exports = nextConfig
  2. augustjk commented on Jun 20, 2023

    @augustjk
    MemberAuthor

    app directory support definitely looks worthwhile to pursue. It does appear that something with the App router breaks Declarative Shadow DOM parsing even for browsers that should support it, so it won't be as simple as updating the entrypoint regex.

    I'll investigate and add updates here.

  3. self-assigned this
    on Jun 20, 2023
  4. moved this to 📋 Triaged in Lit Project Boardon Jun 21, 2023
  5. datradito commented on Aug 3, 2023

    @datradito

    Hello @augustjk , is there any news regarding AppRouter support? to work with /app folder in NextJS 13.
    Thank you.

  6. augustjk commented on Aug 4, 2023

    @augustjk
    MemberAuthor

    @datradito sorry I haven't gotten around to investigating fully yet as we've been focused on other things for the Lit 3.0 release. I can't say for sure when I'll get to it but it's high on my list of things to do after 3.0.

  7. ChellappanRajan commented on Sep 8, 2023

    @ChellappanRajan

    Hi @augustjk, when we consume the @labs/react wrapped web component in a Next.js application and use the app router, we encounter this error. Do you think this could be due to the app's layout?

    image

  8. steveworkman commented on Oct 3, 2023

    @steveworkman
    Contributor

    There's a lot of really good investigation in to this from @ceIia & others on Discord. In short:

    In addition, for component library authors you can add use client to all your components as part of the build process like this https://github.com/shuding/react-wrap-balancer/blob/main/tsup.config.ts#L10-L13

    I bring this up again as I got caught out when creating a new nextjs app and going for the recommended options, which now don't work without use client

    image

    I also played around with bare imports for a simple component and with @lit-labs/[email protected] and @lit/[email protected]
    I was able to see a server render for the component, but it then fell into hydration issues described in #3858 even when matching the reflected attributes correctly.

  9. ceIia commented on Nov 7, 2023

    @ceIia

    hey! a new issue popped up with the latest version of next (14.0.1):

    this issue breaks the build both with importing the enable-lit-ssr.js script from @lit-labs/ssr-react or the withLitSSR() from @lit-labs/nextjs config:

    Error occurred prerendering page "/test/98". Read more: https://nextjs.org/docs/messages/prerender-error
    TypeError: Cannot set property createElement of [object Module] which has only a getter
        at 9485 (/Users/celiabourial/.dev/lit-next-ssr/.next/server/chunks/392.js:48:19)
        at __webpack_require__ (/Users/celiabourial/.dev/lit-next-ssr/.next/server/webpack-runtime.js:1:145)
        at 6823 (/Users/celiabourial/.dev/lit-next-ssr/.next/server/chunks/850.js:1:474)
        at __webpack_require__ (/Users/celiabourial/.dev/lit-next-ssr/.next/server/webpack-runtime.js:1:145)
        at F (/Users/celiabourial/.dev/lit-next-ssr/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:36:6049)
        at $ (/Users/celiabourial/.dev/lit-next-ssr/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:36:4600)
        at rS (/Users/celiabourial/.dev/lit-next-ssr/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:28:27173)
        at nC (/Users/celiabourial/.dev/lit-next-ssr/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:28:52685)
        at nR (/Users/celiabourial/.dev/lit-next-ssr/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:28:57452)
        at nk (/Users/celiabourial/.dev/lit-next-ssr/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:28:49214)
    

    i just tried [email protected] and it builds fine, but with 14.0.1 doesn't (i haven't checked which canary between those two versions breaks it yet though).
    i've updated my repro of the original issue repo on a separate branch linked below.

    → https://github.com/ceIia/lit-next-ssr/tree/next-14

  10. augustjk commented on Nov 7, 2023

    @augustjk
    MemberAuthor

    Oof.. it seems maybe they're doing es modules so monkey patching React.createElement doesn't work.

  11. ceIia commented on Nov 8, 2023

    @ceIia
  12. augustjk commented on Nov 21, 2023

    @augustjk
    MemberAuthor

    I found a discussion in the nextjs repo regarding declarative shadow dom and posted a comment there. vercel/next.js#51717 (comment)

    For now, I think we just won't be able to have custom elements being server rendered with DSD within a React Server Component. Since RSC only get rendered on the server, never client-side, it also means that component definitions won't get shipped if that's the only place it is used.

    I tried a home page without any custom elements, that links to another page that does contain a custom element. The way Next.js handles the client routing is to make an API call to the server which returns the serialized server tree including the RSC. This means the tree with the template element that has a prop called "shadowrootmode" which is re-created client side by Next/React. Of course the browser does not treat this as DSD. The custom element won't even client render because code used in RSC is not shipped to the client and it doesn't "hydrate".

    To use custom elements with the App router in Next.js, I think the only solution for now is to use them in React client components with the 'use client'; directive. I have checked that that actually does SSR for initial page requests, and hydrates properly. Navigating to other pages that have the custom element in a client component also seems to work.

  13. augustjk commented on Nov 21, 2023

    @augustjk
    MemberAuthor

    Something we can do for now would be update the regex used in the plugin for the injection point to allow app folder structure, and document that custom elements to be server rendered should only be used in React client components.

    We also need to figure out what's going on with the module situation in Next.js 14. I think that's a separate issue though. This seems related the error @ceIia was seeing above preactjs/signals#429

  14. Roman86 commented on Feb 9, 2024

    @Roman86

    up =) any luck with this?

  15. gaearon commented on Feb 12, 2024

    @gaearon

    Left a small comment here: vercel/next.js#51717 (reply in thread)

  16. moved this from 📋 Triaged to ✅ Done in Lit Project Boardon Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions