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

Turn on Remix v3_singleFetch future flag #2708

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

Conversation

wizardlyhel
Copy link
Contributor

@wizardlyhel wizardlyhel commented Jan 13, 2025

WHY are these changes introduced?

Turn on Remix v3_singleFetch future flag

Upgrade steps

Remix single fetch migration quick guide: https://remix.run/docs/en/main/start/future-flags#v3_singlefetch
Remix single fetch migration guide: https://remix.run/docs/en/main/guides/single-fetch

  1. In your vite.config.ts, add the single fetch future flag.

    +  declare module "@remix-run/server-runtime" {
    +    interface Future {
    +     v3_singleFetch: true;
    +    }
    +  }
    
      export default defineConfig({
        plugins: [
          hydrogen(),
          oxygen(),
          remix({
            presets: [hydrogen.preset()],
            future: {
              v3_fetcherPersist: true,
              v3_relativeSplatPath: true,
              v3_throwAbortReason: true,
              v3_lazyRouteDiscovery: true,
    +         v3_singleFetch: true,
            },
          }),
          tsconfigPaths(),
        ],
  2. In your entry.server.tsx, add nonce to the <RemixServer>.

    const body = await renderToReadableStream(
      <NonceProvider>
        <RemixServer
          context={remixContext}
          url={request.url}
    +     nonce={nonce}
        />
      </NonceProvider>,
  3. Deprecate json and defer import usage from @shopify/remix-oxygen

    Remove json()/defer() in favor of raw objects.

    Single Fetch supports JSON objects and Promises out of the box, so you can return the raw data from your loader/action functions:

    - import {json} from "@shopify/remix-oxygen";
    
      export async function loader({}: LoaderFunctionArgs) {
        let tasks = await fetchTasks();
    -   return json(tasks);
    +   return tasks;
      }
    - import {defer} from "@shopify/remix-oxygen";
    
      export async function loader({}: LoaderFunctionArgs) {
        let lazyStuff = fetchLazyStuff();
        let tasks = await fetchTasks();
    -   return defer({ tasks, lazyStuff });
    +   return { tasks, lazyStuff };
      }

    If you were using the second parameter of json/defer to set a custom status or headers on your response, you can continue doing so via the new data API:

    -  import {json} from "@shopify/remix-oxygen";
    +  import {data} from "@shopify/remix-oxygen";
    
      export async function loader({}: LoaderFunctionArgs) {
        let tasks = await fetchTasks();
    -    return json(tasks, {
    +    return data(tasks, {
          headers: {
            "Cache-Control": "public, max-age=604800"
          }
        });
      }

WHAT is this pull request doing?

For all templates, remove json(), and defer() Remix response functions and use data() Remix response function when required

HOW to test your changes?

Make sure all templates are working properly without errors across all functions. Make sure:

  • Customers can login with customer account
  • All template examples are working
  • Oxygen deploys are working

Post-merge steps

Checklist

  • I've read the Contributing Guidelines
  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've added a changeset if this PR contains user-facing or noteworthy changes
  • I've added tests to cover my changes
  • I've added or updated the documentation

Copy link
Contributor

shopify bot commented Jan 13, 2025

Oxygen deployed a preview of your hl-single-fetch branch. Details:

Storefront Status Preview link Deployment details Last update (UTC)
custom-cart-method ✅ Successful (Logs) Preview deployment Inspect deployment January 15, 202510:19 PM
metaobjects ✅ Successful (Logs) Preview deployment Inspect deployment January 15, 202510:19 PM
classic-remix ✅ Successful (Logs) Preview deployment Inspect deployment January 15, 202510:19 PM
third-party-queries-caching ✅ Successful (Logs) Preview deployment Inspect deployment January 15, 202510:19 PM
Skeleton (skeleton.hydrogen.shop) ✅ Successful (Logs) Preview deployment Inspect deployment January 15, 202510:19 PM

Learn more about Hydrogen's GitHub integration.

@wizardlyhel wizardlyhel marked this pull request as ready for review January 14, 2025 23:57
Copy link
Contributor

We detected some changes in packages/*/package.json or packages/*/src, and there are no updates in the .changeset.
If the changes are user-facing and should cause a version bump, run npm run changeset add to track your changes and include them in the next release CHANGELOG.
If you are making simple updates to examples or documentation, you do not need to add a changeset.

Comment on lines 62 to 74
/**
* Remix (single-fetch) request objects have different url
* paths when soft navigating. Examples:
*
* /_root.data - home page
* /collections.data - collections page
*
* These url annotations needs to be cleaned up before constructing urls to be passed as
* GET parameters for customer login url
*/
const cleanedPathname = pathname
.replace(/\.data$/, '')
.replace(/^\/_root$/, '/');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this new behaviour covered in any of our tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh good catch - I'll get a test in

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests added

docs/preview/vite.config.js Outdated Show resolved Hide resolved
wizardlyhel and others added 3 commits January 15, 2025 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants