vtex / faststore

Digital commerce toolkit for frontend developers
https://faststore.dev
MIT License
189 stars 60 forks source link

Feat: PLP - multiple templates rewrites #2320

Closed eduardoformiga closed 4 months ago

eduardoformiga commented 5 months ago

What's the purpose of this pull request?

This PR aims to add multiple-page templates for friendly URLs that are added using rewrites. It also add some tests.

The documentation is in this PR:

How it works?

Using Rewrites with PLP template

FastStore enables the use of rewrites from Next.js to create a custom route. It's possible to enable the multiple-page template feature for the custom route as well by adding your rewrites function into the faststore.config.js file as the Next.js doc suggests:

async rewrites() {
  return [
    {
      source: '/my-office',
      destination: `/office`,
    },
  ]
},

or

async rewrites() {
  return {
    beforeFiles: [
      {
        source: '/my-office',
        destination: `/office`,
      },
    ],
    afterFiles: [],
    fallback: [],
  }
},

In the example above, the new source route /my-office will be rewritten to the existing destination route /office, using the /office template without changing the URL. In that way, you can use the existing destination route /office to map this template in the hCMS.

From the version of this PR, it's also possible to use the source route in the hCMS to map the template.

How to test it?

  1. switch to the vendemo account in the cms config file (keeping the storeframework in the faststore.config.js just to keep the catalog). https://github.com/vtex/faststore/blob/37bca87b4f08002bcb74b3734e9b273d8560bee2/packages/core/src/server/cms/index.ts#L45
  2. use this page OR create a new PLP in hCMS and use /test/my-officeor /office in the input from the settings tab Screenshot 2024-05-16 at 12 57 11
  3. publish your page and run the project locally with yarn dev
  4. both URLs (/test/my-officeor /office) should show the new template.

References

PLP doc

vercel[bot] commented 5 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment | Name | Status | Preview | Comments | Updated (UTC) | | :--- | :----- | :------ | :------- | :------ | | **faststore-site** | ⬜️ Ignored ([Inspect](https://vercel.com/faststore/faststore-site/7ALsVDWprm345pknioEFBXBcmJNo)) | [Visit Preview](https://faststore-site-git-feat-multiple-templates-rew-0dbe92-faststore.vercel.app) | | May 24, 2024 2:27pm |
codesandbox-ci[bot] commented 5 months ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.