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

Next 13 example (app/ Directory) #340

Closed
wiesson opened this issue Oct 25, 2022 · 10 comments
Closed

Next 13 example (app/ Directory) #340

wiesson opened this issue Oct 25, 2022 · 10 comments
Labels
enhancement New feature or request nextjs Next.js specific functionality

Comments

@wiesson
Copy link

wiesson commented Oct 25, 2022

Feature request

Next 13 has been released https://nextjs.org/blog/next-13#layouts and a new app structure with data fetching has been introduced (currently in beta).

Is your feature request related to a problem? Please describe.

I tried to adapt but I got errors on server side that the supabase client has problem with the polyfilled fetch provided by next.

Describe the solution you'd like

Just a minimal working example would be nice :)

@wiesson wiesson added the enhancement New feature or request label Oct 25, 2022
@thorwebdev
Copy link
Member

@wiesson would you be able to provide your code and screenshots of the errors you were seeing? I'm working on an example now, but it would be helpful to understand how you approached this and what errors you experienced. Thanks.

@wiesson
Copy link
Author

wiesson commented Oct 26, 2022

import { use } from "react";
import { createClient } from "@supabase/supabase-js";

const supabaseServer = createClient(
  process.env.NEXT_PUBLIC_SUPABASE_URL as string,
  process.env.SUPABASE_SERVICE_KEY as string
);

async function getData() {
  const { data } = await supabase
    .from("bookings_booking")
    .select("*")
    .order("created_at", { ascending: false });

  return data;
}

export default function Page() {
  const bookings = use(getData());
  return (
    <div>
      {bookings((item) => (
        <div key={item.id}>{item.title}</div>
      ))}
    </div>
  );
}

Error

warn  - ../../node_modules/.pnpm/node-fetch@2.6.7/node_modules/node-fetch/lib/index.js
Module not found: Can't resolve 'encoding' in '/Users/wiese/Dev/tm/next-13/node_modules/.pnpm/node-fetch@2.6.7/node_modules/node-fetch/lib'
Warning: Only createServerContext is supported in Server Components.

I tried to monkey-patch node-fetch to version 3.X but then I got ESM Import errors. What works (kind of)

const fetch = (...args) =>
  import("node-fetch").then(({ default: fetch }) => fetch(...args));

const supabaseServer = createClient(
  process.env.NEXT_PUBLIC_SUPABASE_URL as string,
  process.env.SUPABASE_SERVICE_KEY as string,
  {
    global: {
      fetch,
    },
  }
);

I still get the error, but I also get data. I assume that server rendering fails but FE can fetch it

@thorwebdev
Copy link
Member

@wiesson this is working fine for me

// /app/nextjs-13/client-component/page.tsx
import { use } from 'react';
import { createClient } from '@supabase/supabase-js';

const supabase = createClient(
  process.env.NEXT_PUBLIC_SUPABASE_URL as string,
  process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY as string
);

async function getData() {
  const { data } = await supabase.from('users').select('*');

  return data;
}

export default function Page() {
  const data = use(getData());
  return <pre>{JSON.stringify(data, null, 2)}</pre>;
}

With the following versions

"dependencies": {
    "@supabase/supabase-js": "^2.0.4",
    "eslint-config-next": "^13.0.0",
    "next": "^13.0.0",
    "react": "18.2.0",
    "react-dom": "18.2.0"
  },

What versions are you using?

@thorwebdev
Copy link
Member

Also note that you should not use SUPABASE_SERVICE_KEY client-side. It sounds like everything is being server-rendered now, so maybe it's fine, but I'm not yet entirely sure if the env vars might be exposed to the client or not.

@wiesson
Copy link
Author

wiesson commented Oct 26, 2022

Also note that you should not use SUPABASE_SERVICE_KEY client-side. It sounds like everything is being server-rendered now, so maybe it's fine, but I'm not yet entirely sure if the env vars might be exposed to the client or not.

It was just for testing purposes - I'm trying to find if it runs on the client or on the server. I still have the same issues as mentioned initially. Even if I directly copy your example:

"dependencies": {
    "@supabase/supabase-js": "^2.0.4",
    "next": "13.0.0",
    "react": "18.2.0",
    "react-dom": "18.2.0"
  },
  "devDependencies": {
    "@types/node": "18.11.5",
    "@types/react": "18.0.23",
    "eslint": "8.26.0",
    "eslint-config-next": "13.0.0",
    "typescript": "4.8.4"
  }

node LTS (v16.18.0)
pnpm 7.14.0

@thorwebdev
Copy link
Member

@wiesson any chance your code is available on GitHub somewhere? I'm currently not able to reproduce this. You can find my code here: https://github.com/supabase/auth-helpers/pull/342/files

image

@thorwebdev thorwebdev added the nextjs Next.js specific functionality label Oct 26, 2022
@wiesson
Copy link
Author

wiesson commented Oct 26, 2022

Thanks for the repo! I'll test it.

https://github.com/wiesson/supabase-next-13

warn  - ./node_modules/.pnpm/node-fetch@2.6.7/node_modules/node-fetch/lib/index.js
Module not found: Can't resolve 'encoding' in '/Users/wiese/Dev/testing/supabase-next-13/node_modules/.pnpm/node-fetch@2.6.7/node_modules/node-fetch/lib'

I saw that you explicitly called your route "client-component" but I can't see a "use client" - how do you know that it will be only loaded client-side? I suspect that you won't have any fetch errors because supabase will use the native browser fetch instead 🤔

@thorwebdev
Copy link
Member

thorwebdev commented Oct 26, 2022

I'm missing it because the guide as well as your snippet above aren't using it https://beta.nextjs.org/docs/data-fetching/fetching#example-fetch-and-use-in-client-components. Refactoring now.

In the guide it does however say:

fetch is currently not supported in Client Components and may trigger multiple re-renders. For now, if you need to fetch data in a Client Component, we recommend using a third-party library such as SWR.

@thorwebdev
Copy link
Member

Based on https://beta.nextjs.org/docs/data-fetching/fetching#example-fetch-and-use-in-client-components it sounds like fetch and use should not be used in client components. So I'm not sure why they have the example there. I'll check with the vercel team.

It does work fine when adding use client, but it constantly refetches. I guess that's why it shouldn't be used client side and you should rather stick with SWR etc.

@thorwebdev
Copy link
Member

@wiesson I'm able to reproduce the warning with your repo, but it should be safe to ignore as it's an optional dependency in node-fetch and supabase-js defaults to the global fetch object when there is one: https://github.com/supabase/supabase-js/blob/master/src/lib/fetch.ts#L9-L15

Therefore should be safe to ignore, but nonetheless, I've filed it here: supabase/supabase-js#612

Thanks for the report!

@thorwebdev thorwebdev linked a pull request Oct 26, 2022 that will close this issue
@wiesson wiesson closed this as completed Oct 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request nextjs Next.js specific functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants