Skip to content

Commit deca78f

Browse files
authored
docs: updated next.js starter docs (medusajs#5748)
* docs: updated next.js starter docs * fix lint errors * added migration script
1 parent 5f90515 commit deca78f

File tree

2 files changed

+133
-85
lines changed

2 files changed

+133
-85
lines changed

www/apps/docs/content/plugins/search/meilisearch.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ In this document, you’ll learn how to install the [MeiliSearch plugin](https:/
1313

1414
MeiliSearch also provides a pleasant developer experience, as it is extremely intuitive and newcomer-friendly. So, even if you're new to the search engine ecosystem, [their documentation](https://docs.meilisearch.com/) is resourceful enough for everyone to go through and understand.
1515

16-
Through Medusa's flexible plugin system, it is possible to add a search engine to your Medusa backend and storefront using MeiliSearch with just a few steps.
16+
Through Medusa's flexible plugin system, it's possible to add a search engine to your Medusa backend and storefront using MeiliSearch with just a few steps.
1717

1818
---
1919

@@ -27,7 +27,7 @@ It is required to have a Medusa backend installed before starting with this docu
2727

2828
You must install MeiliSearch to use it with Medusa. You can follow [this documentation to install MeiliSearch](https://docs.meilisearch.com/learn/getting_started/quick_start.html#setup-and-installation) either locally or on a cloud.
2929

30-
Furthermore, you should create a master key for your MeiliSearch instance. If you don’t have one created, follow [this guide](https://docs.meilisearch.com/learn/security/master_api_keys.html#protecting-a-meilisearch-instance) to create a master key.
30+
Furthermore, you should create a master key for your MeiliSearch instance. If you don’t have one created, follow [this guide](https://www.meilisearch.com/docs/learn/security/master_api_keys#protecting-a-meilisearch-instance) to create a master key.
3131

3232
---
3333

@@ -46,9 +46,10 @@ MEILISEARCH_HOST=<YOUR_MEILISEARCH_HOST>
4646
MEILISEARCH_API_KEY=<YOUR_MASTER_KEY>
4747
```
4848

49-
Where `<YOUR_MEILISEARCH_HOST>` is the host of your MeiliSearch instance. By default, if MeiliSearch is installed locally, the host is `http://127.0.0.1:7700`.
49+
Where:
5050

51-
`<YOUR_MASTER_KEY>` is the master key of your MeiliSearch instance.
51+
- `<YOUR_MEILISEARCH_HOST>` is the host of your MeiliSearch instance. By default, if MeiliSearch is installed locally, the host is `http://127.0.0.1:7700`.
52+
- `<YOUR_MASTER_KEY>` is the master key of your MeiliSearch instance.
5253

5354
Finally, in `medusa-config.js` add the following item into the `plugins` array:
5455

@@ -90,7 +91,10 @@ const plugins = [
9091
displayedAttributes,
9192
},
9293
primaryKey,
93-
transformer,
94+
transformer: (product) => ({
95+
id: product.id,
96+
// other attributes...
97+
}),
9498
},
9599
},
96100
},
@@ -141,10 +145,6 @@ const plugins = [
141145
],
142146
},
143147
primaryKey: "id",
144-
transformer: (product) => ({
145-
id: product.id,
146-
// other attributes...
147-
}),
148148
},
149149
},
150150
},

www/apps/docs/content/starters/nextjs-medusa-starter.mdx

Lines changed: 124 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -141,60 +141,31 @@ Your Next.js Starter Storefront is now running at `localhost:8000`
141141

142142
---
143143

144-
## Development Notes
145-
146-
### Changing Medusa Backend URL
147-
148-
By default, the Medusa backend runs at `http://localhost:9000`. This value is defined in your Next.js starter Storefront under the environment variable `NEXT_PUBLIC_MEDUSA_BACKEND_URL`.
149-
150-
If you need to change the URL of your Medusa backend because you changed the backend's default port or because you deployed your backend, change the value of `NEXT_PUBLIC_MEDUSA_BACKEND_URL` (typically in `.env.local`) to your backend URL. For example:
151-
152-
```bash
153-
NEXT_PUBLIC_MEDUSA_BACKEND_URL=http://localhost:9001
154-
```
155-
156-
### Using Product Module
157-
158-
:::note
159-
160-
The Product Module is currently in beta. You can learn more about it [here](../experimental/product/install-nodejs.md). In addition, the product module in the Next.js storefront can't be used without the Medusa backend at the moment.
161-
162-
:::
144+
## Storefront Features
163145

164-
This starter has full support for the serverless Product Module for retrieving and manipulating product data directly from a serverless function. This keeps your product logic close to the frontend, making it easy to customize or extend Medusa's core functionality from within your Next.js project.
146+
### Featured products on homepage
165147

166-
By default, this starter uses the standard Medusa API for product and collection retrieval.
148+
Your store's products are showcased on the homepage. If you don't see any products, make sure to [create collections and add products to them](../user-guide/products/collections.mdx).
167149

168-
To enable the Product Module, first, make sure to set the following environment variables:
150+
![Featured products on the homepage](https://res.cloudinary.com/dza7lstvk/image/upload/v1701115394/Medusa%20Docs/Screenshots/Screenshot_2023-11-27_at_8.03.02_PM_ekystg.png)
169151

170-
- `PRODUCT_POSTGRES_URL`: the URL of your PostgreSQL databsae.
171-
- `NEXT_PUBLIC_BASE_URL`: the URL of your storefront's base URL. For exmaple, if you're running it locally, it should be `http://localhost:8000`.
152+
### View all products and manage your cart
172153

173-
Then, enable the `productModule` feature flag in `store.config.json`:
154+
![All Products Page](https://res.cloudinary.com/dza7lstvk/image/upload/v1701115070/Medusa%20Docs/Screenshots/Screenshot_2023-11-27_at_7.40.04_PM_hnmbwa.png)
174155

175-
```json title="store.config.json"
176-
{
177-
"features": {
178-
// other features...
179-
"productModule": true
180-
}
181-
}
182-
183-
```
156+
### Customer authentication and profiles
184157

185-
You can now test it out now. Make sure the Medusa backend is running, then start (or restart) your Next.js storefront:
158+
![Customer Profile](https://res.cloudinary.com/dza7lstvk/image/upload/v1701115136/Medusa%20Docs/Screenshots/Screenshot_2023-11-27_at_7.58.43_PM_xfdnnp.png)
186159

187-
```bash npm2yarn
188-
npm run start
189-
```
160+
### Full checkout workflow
190161

191-
If you go to `localhost:8000`, all product and collection data should now be coming from the module. The Product Module routes are all in the `src/app/api` directory for you to customize to your use case.
162+
![Checkout Page](https://res.cloudinary.com/dza7lstvk/image/upload/v1701115205/Medusa%20Docs/Screenshots/Screenshot_2023-11-27_at_7.59.55_PM_psdjwq.png)
192163

193-
To opt out of using the product module, simply set the `productModule` feature flag in `store.config.json` to `false` and restart the server.
164+
---
194165

195-
### Toggle Search Engine Feature
166+
## Toggle Search Engine Feature
196167

197-
The Next.js Starter Storefront by default is compatible with MeiliSearch.
168+
The Next.js Starter Storefront by default is compatible with the MeiliSearch plugin.
198169

199170
To enable or disable the search engine, change the value of the feature in `store.config.json`:
200171

@@ -206,25 +177,25 @@ To enable or disable the search engine, change the value of the feature in `stor
206177
}
207178
```
208179

209-
Then, restart your Next.js backend. Depending on whether you enabled or disabled the search engine, the search bar will appear or disappear in the navigation bar accordingly.
180+
Then, restart your Next.js backend. Depending on whether you enabled or disabled the search engine, the "Search" link in the navigation bar will appear or disappear accordingly.
210181

211182
### MeiliSearch Integration
212183

213-
If you have the search engine feature enabled, it is expected that you have installed the MeiliSearch plugin on your Medusa backend. If not, [follow this guide to install it](../plugins/search/meilisearch.md).
184+
If you have the search engine feature enabled, it's expected that you have installed the MeiliSearch plugin on your Medusa backend. If not, [follow this guide to install it](../plugins/search/meilisearch.md).
214185

215186
In your Next.js Starter Storefront, set the environment variables necessary for the MeiliSearch integration:
216187

217188
```json
218189
NEXT_PUBLIC_SEARCH_ENDPOINT=<YOUR_MEILISEARCH_URL>
219-
NEXT_PUBLIC_SEARCH_API_KEY=<YOUR_API_KEY>
220190
NEXT_PUBLIC_INDEX_NAME=products
191+
NEXT_PUBLIC_SEARCH_API_KEY=<YOUR_API_KEY>
221192
```
222193

223-
`<YOUR_MEILISEARCH_URL>` is the URL MeiliSearch is running on. The default is `http://127.0.0.1:7700`.
224-
225-
`NEXT_PUBLIC_INDEX_NAME` is the index name of the products in MeiliSearch. By default, it’s `products`.
194+
Where:
226195

227-
`<YOUR_API_KEY>` is the API key used to search through MeiliSearch indexes. To create a new API Key, make sure that the MeiliSearch service is running and send the following request:
196+
- `<YOUR_MEILISEARCH_URL>` is the URL MeiliSearch is running on. The default is `http://127.0.0.1:7700`.
197+
- `NEXT_PUBLIC_INDEX_NAME` is the index name of the products in MeiliSearch. By default, it’s `products`.
198+
- `<YOUR_API_KEY>` is the API key used to search through MeiliSearch indexes. To create a new API Key, make sure that the MeiliSearch service is running and send the following request:
228199

229200
```bash
230201
curl \
@@ -239,33 +210,40 @@ curl \
239210
}'
240211
```
241212

242-
Make sure to replace `<MEILISEARCH_URL>` with the URL MeiliSearch is running on and `<MEILISEARCH_MASTER_KEY>` with your MeiliSearch [master key](https://docs.meilisearch.com/learn/security/master_api_keys.html#protecting-a-meilisearch-instance).
213+
Make sure to replace `<MEILISEARCH_URL>` with the URL MeiliSearch is running on and `<MEILISEARCH_MASTER_KEY>` with your MeiliSearch [master key](https://www.meilisearch.com/docs/learn/security/master_api_keys#protecting-a-meilisearch-instance).
243214

244-
Then, restart the Next.js backend. You’ll be able to search through available products by clicking the search icon in the navigation bar.
215+
Then, restart the Next.js storefront. You’ll be able to search through available products by clicking the search icon in the navigation bar.
245216

246-
:::note
217+
:::tip
247218

248-
To make sure the Next.js Starter Storefront properly displays the products in the search result, include in the `displayedAttributes` setting of the MeiliSearch plugin on the Medusa backend at least the fields `title`, `handle`, `description`, and `thumbnail`.
219+
To make sure the Next.js Storefront properly displays the products in the search result, include in the `displayedAttributes` setting of the MeiliSearch plugin on the Medusa backend at least the fields `title`, `handle`, `description`, and `thumbnail`.
249220

250221
:::
251222

252-
![Search Result on Next.js Starter Storefront](https://res.cloudinary.com/dza7lstvk/image/upload/v1668003191/Medusa%20Docs/Screenshots/gQVWvH2_h1ljig.png)
223+
![Search Result on Next.js Starter Storefront](https://res.cloudinary.com/dza7lstvk/image/upload/v1701112725/Medusa%20Docs/Screenshots/Screenshot_2023-11-27_at_7.18.09_PM_iozjt0.png)
253224

254225
### Algolia Integration
255226

256227
Instead of using the default MeiliSearch search engine, you can switch to using Algolia. Make sure you start by installing the Algolia plugin on your Medusa backend. You can do it by [following this guide](../plugins/search/algolia.md).
257228

258-
In your Next.js Starter Storefront, set the environment variables necessary for the Algolia integration:
229+
In your Next.js Starter Storefront, install the `algoliasearch` package in your Next.js storefront:
230+
231+
```bash npm2yarn
232+
npm install algoliasearch
233+
```
234+
235+
Then, set the environment variables necessary for the Algolia integration:
259236

260237
```bash
261238
NEXT_PUBLIC_SEARCH_APP_ID=<YOUR_APP_ID>
262239
NEXT_PUBLIC_SEARCH_API_KEY=<YOUR_SEARCH_API_KEY>
263240
NEXT_PUBLIC_INDEX_NAME=products
264241
```
265242

266-
Where `<YOUR_APP_ID>` and `<YOUR_SEARCH_API_KEY>` are the Algolia App ID and Algolia Search API Key respectively. You can retrieve them from Algolia by going to [API Keys](https://www.algolia.com/account/api-keys/all) in your account settings.
243+
Where:
267244

268-
`NEXT_PUBLIC_INDEX_NAME` is the index name of the products in Algolia. By default, it’s `products`.
245+
- `<YOUR_APP_ID>` and `<YOUR_SEARCH_API_KEY>` are the Algolia App ID and Algolia Search API Key respectively. You can retrieve them from Algolia by going to [API Keys](https://www.algolia.com/account/api-keys/all) in your account settings.
246+
- `NEXT_PUBLIC_INDEX_NAME` is the index name of the products in Algolia. By default, it’s `products`.
269247

270248
Next, change the content of `src/lib/search-client.ts` to the following:
271249

@@ -282,9 +260,35 @@ export const SEARCH_INDEX_NAME =
282260
process.env.NEXT_PUBLIC_INDEX_NAME || "products"
283261
```
284262

285-
Then, restart the Next.js backend. You’ll be able to search through available products by clicking the search icon in the navigation bar.
263+
Next, change the content of `src/app/(main)/search/actions.ts` to the following:
264+
265+
```ts title="src/app/(main)/search/actions.ts"
266+
"use server"
267+
268+
import {
269+
searchClient,
270+
SEARCH_INDEX_NAME,
271+
} from "@lib/search-client"
272+
273+
/**
274+
* Uses MeiliSearch or Algolia to search for a query
275+
* @param {string} query - search query
276+
*/
277+
export async function search(query: string) {
278+
const index = searchClient.initIndex(SEARCH_INDEX_NAME)
279+
const { hits } = await index.search(query)
280+
281+
return hits
282+
}
283+
```
286284

287-
![Search Pop-up in Next.js Starter Storefront](https://res.cloudinary.com/dza7lstvk/image/upload/v1668003205/Medusa%20Docs/Screenshots/ZLgX5Ad_po6a4n.png)
285+
Finally, restart the Next.js backend. You’ll be able to search through available products by clicking the search icon in the navigation bar.
286+
287+
![Search Pop-up in Next.js Starter Storefront](https://res.cloudinary.com/dza7lstvk/image/upload/v1701112725/Medusa%20Docs/Screenshots/Screenshot_2023-11-27_at_7.18.09_PM_iozjt0.png)
288+
289+
---
290+
291+
## Payment Integrations
288292

289293
### Stripe Payment Integration
290294

@@ -300,8 +304,6 @@ Make sure to replace `<YOUR_PUBLISHABLE_KEY>` with your Stripe publishable key.
300304

301305
If you restart your Next.js backend you should be able to pay with Stripe on checkout.
302306

303-
![Pay with Stripe on Checkout](https://res.cloudinary.com/dza7lstvk/image/upload/v1668003250/Medusa%20Docs/Screenshots/h5mWdJT_yjogcq.png)
304-
305307
### PayPal Payment Integration
306308

307309
PayPal integration is supported by default. Make sure you have PayPal installed and enabled on your Medusa backend first. You can [follow this guide to learn how to install it](../plugins/payment/paypal.md).
@@ -316,15 +318,19 @@ Make sure to replace `<YOUR_CLIENT_ID>` with your PayPal client ID. You can retr
316318

317319
If you restart your Next.js backend you should be able to pay with PayPal on checkout.
318320

319-
![Pay with PayPal on Checkout](https://res.cloudinary.com/dza7lstvk/image/upload/v1668003264/Medusa%20Docs/Screenshots/F8OvsOJ_mlx0le.png)
321+
---
320322

321-
### Customization
323+
## Development Notes
322324

323-
To customize the pages of the storefront, you can customize the files under the `src/pages` directory.
325+
### Changing Medusa Backend URL
324326

325-
To customize the components used in the storefront, you can customize the files under the `src/modules` directory.
327+
By default, the Medusa backend runs at `http://localhost:9000`. This value is defined in your Next.js starter Storefront under the environment variable `NEXT_PUBLIC_MEDUSA_BACKEND_URL`.
326328

327-
To customize the styles of the storefront, you can customize the `src/styles` directory.
329+
If you need to change the URL of your Medusa backend because you changed the backend's default port or because you deployed your backend, change the value of `NEXT_PUBLIC_MEDUSA_BACKEND_URL` (typically in `.env.local`) to your backend URL. For example:
330+
331+
```bash
332+
NEXT_PUBLIC_MEDUSA_BACKEND_URL=http://localhost:9001
333+
```
328334

329335
### Change Port
330336

@@ -347,25 +353,67 @@ Then, on your backend, update the environment variable `STORE_CORS` to the URL w
347353
STORE_CORS=http://localhost:<PORT>
348354
```
349355

350-
### Development Resources
356+
### Using Serverless Modules
351357

352-
You can learn more about development with Next.js through [their documentation](https://nextjs.org/docs/getting-started).
358+
:::note
353359

354-
---
360+
Serverless Modules are currently in beta and, at the moment, they can't be used in the Next.js storefront without the Medusa backend running.
355361

356-
## Storefront Features
362+
:::
357363

358-
- View all products and manage your cart.
364+
This starter fully supports the experimental [Product](../experimental/product/overview.mdx) and [Pricing](../experimental/pricing/overview.mdx) modules for retrieving and manipulating product and pricing data directly from a serverless function. This keeps your logic close to the frontend, making it easy to customize or extend Medusa's core functionality from within your Next.js project.
359365

360-
![All Products Page](https://res.cloudinary.com/dza7lstvk/image/upload/v1668003278/Medusa%20Docs/Screenshots/1vLAYbH_sulxrr.png)
366+
By default, this starter uses the standard Medusa API for product, collection, and pricing retrieval.
367+
368+
To enable the usage of the experimental modules, first, set the following environment variables:
369+
370+
- `POSTGRES_URL`: the URL of your PostgreSQL databsae.
371+
- `NEXT_PUBLIC_BASE_URL`: the URL of your storefront's base URL. For exmaple, if you're running it locally, it should be `http://localhost:8000`.
361372

362-
- Customer authentication and profiles.
373+
Then, set the following environment variable in both the Next.js storefront and the [Medusa backend](../experimental/index.md#enabling-experimental-features):
363374

364-
![Customer Profile](https://res.cloudinary.com/dza7lstvk/image/upload/v1668003287/Medusa%20Docs/Screenshots/etW3b3L_wccrez.png)
375+
:::warning
376+
377+
This is a one way process. Once you opt in to these features and update your database, there's no way back. Proceed with caution.
378+
379+
:::
380+
381+
```bash
382+
MEDUSA_FF_MEDUSA_V2=true
383+
```
365384

366-
- Full checkout workflow.
385+
Finally, run migrations in your Medusa backend using the following commands:
367386

368-
![Checkout Page](https://res.cloudinary.com/dza7lstvk/image/upload/v1668003296/Medusa%20Docs/Screenshots/VC8SYfb_eowjno.png)
387+
```bash
388+
npx medusa migrations run
389+
node node_modules/@medusajs/medusa/dist/scripts/migrate-to-pricing-module.js
390+
```
391+
392+
You can now test it out now. Make sure the Medusa backend is running, then start (or restart) your Next.js storefront:
393+
394+
```bash npm2yarn
395+
npm run start
396+
```
397+
398+
If you go to `localhost:8000`, all product and collection data should now be coming from the module. The Product Module routes are all in the `src/app/api` directory for you to customize to your use case.
399+
400+
:::info[Deploying to Vercel]
401+
402+
If you're not planning on using the serverless modules, you might encounter errors when deploying to Vercel. You can safely delete or exclude `the src/app/api` folder before deploying. The API routes are only used by the serverless modules.
403+
404+
:::
405+
406+
### Customization
407+
408+
To customize the pages of the storefront, you can customize the files under the `src/app` directory.
409+
410+
To customize the components used in the storefront, you can customize the files under the `src/modules` directory.
411+
412+
To customize the styles of the storefront, you can customize the `src/styles` directory.
413+
414+
### Development Resources
415+
416+
You can learn more about development with Next.js through [their documentation](https://nextjs.org/docs/getting-started).
369417

370418
---
371419

0 commit comments

Comments
 (0)