Building Production-Grade Web Applications with Supabase – Part 1
(This post was originally published on Baby Programmer)
I'm working through David Lorenz's book Building Production-Grade Web Applications with Supabase (affiliate link) and just finished Chapter 2 - Setting Up Supabase with Next.js. I've run into a few issues and figured I'd share them along with how I've fixed them.
Section: Initializing and testing the base Supabase JavaScript client within Next.js
Error: Parsing ecmascript source code failed
One is instructed to use the following code to check for Supabase buckets:
useEffect(() => { const supabase = createSupabaseClient(); supabase.storage.listBuckets().then((result) => console.log("Bucket List", result); }); }, []);
Unfortunately, this will result in the following error:
⨯ ./src/app/page.js:9:5 Parsing ecmascript source code failed 7 | supabase.storage.listBuckets().then((result) => 8 | console.log("Bucket List", result) > 9 | }); | ^ 10 | }, []); 11 | 12 | return ( Expected ',', got '}'
Fortunately, the fix is quite simple, add an opening brace immediately following .then((result) => :
useEffect(() => { const supabase = createSupabaseClient(); supabase.storage.listBuckets().then((result) => { console.log("Bucket List", result) }); }, []);
Error: ReferenceError: useEffect is not defined
Once the above error has been resolved we hit our next one:
⨯ ReferenceError: useEffect is not defined at Home (src/app/page.js:5:2) 3 | 4 | export default function Home() { > 5 | useEffect(() => { | ^ 6 | const supabase = createSupabaseClient(); 7 | supabase.storage.listBuckets().then((result) => { 8 | console.log("Bucket List", result) { digest: '3325505329' }
The issue is that we haven't imported useEffect from React for use on this page. Doing so is simple, add an import for useEffect immediately after the import for Image:
import Image from "next/image"; import { useEffect } from "react"; import { createSupabaseClient } from "@/supabase-utils/client";
Error: Ecmascript file had an error
You'll be immediately greeted with yet another error:
⨯ ./src/app/page.js:2:10 Ecmascript file had an error 1 | import Image from "next/image"; > 2 | import { useEffect } from "react"; | ^^^^^^^^^ 3 | import { createSupabaseClient } from '@/supabase-utils/client'; 4 | 5 | export default function Home() { You're importing a component that needs `useEffect`. This React hook only works in a client component. To fix, mark the file (or its parent) with the `"use client"` directive. Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
Luckily, the solution is once again quite simple. At the top of the file add "use client"; like so:
"use client"; import Image from "next/image";
Section: Using the Supabase client with Pages Router and App Router
Subsection: Utilizing createBrowserClient on the frontend
The first issue we run into isn't really an error, its more that some of the instructions may not be entirely clear to some readers. The reader is instructed to update the createSupabaseClient function to use createBrowserClient like so:
import { createBrowserClient } from "@supabase/ssr"; export const createSupabaseClient = () => createBrowserClient(...);
Where that ... is one shouldn't literally place ..., rather one should insert the contents that previously were within createClient(), e.g. the final code should look like:
import { createBrowserClient } from "@supabase/ssr"; export const createSupabaseClient = () => createBrowserClient( process.env.NEXT_PUBLIC_SUPABASE_URL, process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY );
We are then instructed to make two relatively small adjustments to the client.js file:
We rename the file from client.js to browserClient.js
We rename createSupabaseClient to getSupabaseBrowserClient
But this creates some additional work for us, unless your editor picks up and auto-corrects the changes:
First, we need to update our import in page.js:
import { getSupabaseBrowserClient } from "@/supabase-utils/browserClient";
Next, we need to update our instantiation of the client in page.js:
const supabase = getSupabaseBrowserClient();
We are told that:
"In VS Code, you can press F2 to rename createSupabaseClient. It will automatically be renamed across your project. If you change a filename, VS Code should also pick up the references and refactor the path in other files accordingly."
(I was using Cursor and the changes were not picked up...could certainly have been the connection between the chair and the keyboard in this case)
Subsection: Utilizing createServerClient on the backend
This section is quite long and a bit confusing, as Lorenz acknowledges.
There is one block of code that is particularly confusing:
useEffect(() => { const supabase = createSupabaseClient(); supabase.storage.listBuckets().then((result) => console.log("Bucket List", result); }); }, []);
At first glance it can seem like we are performing the same step twice. It's easy to overlook that the first instance is modifying the request cookies while the second is modifying the response cookies.
The Rest
I skipped the section on using createServerClient with the Pages Router, I'm working on a greenfield project at the moment, if I need to work with a Next.js using Pages Router I can always revisit.
The section on Connecting directly to the database is fairly basic, if you are familiar with SQL db's it makes intuitive sense.
The section on using TypeScript is primarily about running a single command to generate (and then regenerate, as needed) type definitions.
The chapter closes out with basic examples of creating a client for Nuxt 3 (Vue) and Python.
The End
And that's the conclusion of chapter 2. Now wasn't that post just gripping?
The above is the detailed content of Building Production-Grade Web Applications with Supabase – Part 1. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
