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

[DO NOT MERGE] Single Fetch Experiment #2108

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions examples/single-fetch/app/entry.server.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type {EntryContext} from '@shopify/remix-oxygen';
import {RemixServer} from '@remix-run/react';
import isbot from 'isbot';
import {renderToReadableStream} from 'react-dom/server';
import {createContentSecurityPolicy} from '@shopify/hydrogen';

export default async function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext,
) {
const {nonce, header, NonceProvider} = createContentSecurityPolicy();

const body = await renderToReadableStream(
<NonceProvider>
{/***********************************************/
/********** EXAMPLE UPDATE STARTS ************/}
<RemixServer context={remixContext} url={request.url} nonce={nonce} />
{/********** EXAMPLE UPDATE END ************/
/***********************************************/}
</NonceProvider>,
{
nonce,
signal: request.signal,
onError(error) {
// eslint-disable-next-line no-console
console.error(error);
responseStatusCode = 500;
},
},
);

if (isbot(request.headers.get('user-agent'))) {
await body.allReady;
}

responseHeaders.set('Content-Type', 'text/html');
responseHeaders.set('Content-Security-Policy', header);

return new Response(body, {
headers: responseHeaders,
status: responseStatusCode,
});
}
16 changes: 16 additions & 0 deletions examples/single-fetch/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "example-single-fetch",
"private": true,
"prettier": "@shopify/prettier-config",
"scripts": {
"build": "shopify hydrogen build --diff",
"dev": "shopify hydrogen dev --codegen --diff",
"preview": "shopify hydrogen preview --build --diff",
"lint": "eslint --no-error-on-unmatched-pattern --ext .js,.ts,.jsx,.tsx .",
"typecheck": "tsc --noEmit",
"codegen": "shopify hydrogen codegen"
},
"dependencies": {
"@shopify/cli-hydrogen": "*"
}
}
26 changes: 26 additions & 0 deletions examples/single-fetch/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"include": ["./**/*.d.ts", "./**/*.ts", "./**/*.tsx"],
"compilerOptions": {
"lib": ["DOM", "DOM.Iterable", "ES2022"],
"isolatedModules": true,
"esModuleInterop": true,
"jsx": "react-jsx",
"moduleResolution": "Bundler",
"resolveJsonModule": true,
"module": "ES2022",
"target": "ES2022",
"strict": true,
"allowJs": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
"baseUrl": ".",
"types": [
"@shopify/oxygen-workers-types",
"@remix-run/react/future/single-fetch.d.ts"
],
"paths": {
"~/*": ["app/*"]
},
"noEmit": true
}
}
42 changes: 42 additions & 0 deletions examples/single-fetch/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {defineConfig} from 'vite';
import {hydrogen} from '@shopify/hydrogen/vite';
import {oxygen} from '@shopify/mini-oxygen/vite';
import {vitePlugin as remix} from '@remix-run/dev';
import tsconfigPaths from 'vite-tsconfig-paths';

export default defineConfig({
plugins: [
hydrogen(),
oxygen(),
remix({
presets: [hydrogen.preset()],
future: {
v3_fetcherPersist: true,
v3_relativeSplatPath: true,
v3_throwAbortReason: true,
unstable_singleFetch: true,
},
}),
tsconfigPaths(),
],
build: {
// Allow a strict Content-Security-Policy
// withtout inlining assets as base64:
assetsInlineLimit: 0,
},
ssr: {
optimizeDeps: {
/**
* Include dependencies here if they throw CJS<>ESM errors.
* For example, for the following error:
*
* > ReferenceError: module is not defined
* > at /Users/.../node_modules/example-dep/index.js:1:1
*
* Include 'example-dep' in the array below.
* @see https://vitejs.dev/config/dep-optimization-options
*/
include: [],
},
},
});
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"examples/multipass",
"examples/optimistic-cart-ui",
"examples/partytown",
"examples/single-fetch",
"examples/subscriptions",
"examples/third-party-queries-caching",
"examples/analytics",
Expand Down
4 changes: 3 additions & 1 deletion packages/hydrogen/src/customer/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ function defaultAuthStatusHandler(request: CrossRuntimeRequest) {

const redirectTo =
DEFAULT_LOGIN_URL +
`?${new URLSearchParams({return_to: pathname}).toString()}`;
`?${new URLSearchParams({
return_to: pathname.replace(/\.data$/, ''), // for single fetch
}).toString()}`;

return redirect(redirectTo);
}
Expand Down
5 changes: 4 additions & 1 deletion packages/mini-oxygen/src/common/compat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export const OXYGEN_COMPAT_PARAMS = {
compatibilityFlags: ['streams_enable_constructors' as const],
compatibilityFlags: [
'streams_enable_constructors' as const,
'http_headers_getsetcookie' as any,
],
compatibilityDate: '2022-10-31' as const,
};
45 changes: 31 additions & 14 deletions templates/skeleton/app/components/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,46 @@
import {NavLink} from '@remix-run/react';
import {Await, NavLink} from '@remix-run/react';
import {Suspense} from 'react';
import type {FooterQuery, HeaderQuery} from 'storefrontapi.generated';
import {useRootLoaderData} from '~/lib/root-data';

interface FooterProps {
footer: Promise<FooterQuery>;
header: HeaderQuery;
publicStoreDomain: string;
}

export function Footer({
menu,
shop,
}: FooterQuery & {shop: HeaderQuery['shop']}) {
footer: footerPromise,
header,
publicStoreDomain,
}: FooterProps) {
return (
<footer className="footer">
{menu && shop?.primaryDomain?.url && (
<FooterMenu menu={menu} primaryDomainUrl={shop.primaryDomain.url} />
)}
</footer>
<Suspense>
<Await resolve={footerPromise}>
{(footer) => (
<footer className="footer">
{footer.menu && header.shop.primaryDomain?.url && (
<FooterMenu
menu={footer.menu}
primaryDomainUrl={header.shop.primaryDomain.url}
publicStoreDomain={publicStoreDomain}
/>
)}
</footer>
)}
</Await>
</Suspense>
);
}

function FooterMenu({
menu,
primaryDomainUrl,
publicStoreDomain,
}: {
menu: FooterQuery['menu'];
primaryDomainUrl: HeaderQuery['shop']['primaryDomain']['url'];
menu: Awaited<FooterProps['footer']>['menu'];
primaryDomainUrl: FooterProps['header']['shop']['primaryDomain']['url'];
publicStoreDomain: string;
}) {
const {publicStoreDomain} = useRootLoaderData();

return (
<nav className="footer-menu" role="navigation">
{(menu || FALLBACK_FOOTER_MENU).items.map((item) => {
Expand Down
24 changes: 17 additions & 7 deletions templates/skeleton/app/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import {Await, NavLink} from '@remix-run/react';
import {Suspense} from 'react';
import type {HeaderQuery} from 'storefrontapi.generated';
import type {LayoutProps} from '~/components/Layout';
import {useRootLoaderData} from '~/lib/root-data';
import type {HeaderQuery, CartApiQueryFragment} from 'storefrontapi.generated';
import {useAside} from '~/components/Aside';

type HeaderProps = Pick<LayoutProps, 'header' | 'cart' | 'isLoggedIn'>;
interface HeaderProps {
header: HeaderQuery;
cart: Promise<CartApiQueryFragment | null>;
isLoggedIn: Promise<boolean>;
publicStoreDomain: string;
}

type Viewport = 'desktop' | 'mobile';

export function Header({header, isLoggedIn, cart}: HeaderProps) {
export function Header({
header,
isLoggedIn,
cart,
publicStoreDomain,
}: HeaderProps) {
const {shop, menu} = header;
return (
<header className="header">
Expand All @@ -20,6 +28,7 @@ export function Header({header, isLoggedIn, cart}: HeaderProps) {
menu={menu}
viewport="desktop"
primaryDomainUrl={header.shop.primaryDomain.url}
publicStoreDomain={publicStoreDomain}
/>
<HeaderCtas isLoggedIn={isLoggedIn} cart={cart} />
</header>
Expand All @@ -30,12 +39,13 @@ export function HeaderMenu({
menu,
primaryDomainUrl,
viewport,
publicStoreDomain,
}: {
menu: HeaderProps['header']['menu'];
primaryDomainUrl: HeaderQuery['shop']['primaryDomain']['url'];
primaryDomainUrl: HeaderProps['header']['shop']['primaryDomain']['url'];
viewport: Viewport;
publicStoreDomain: HeaderProps['publicStoreDomain'];
}) {
const {publicStoreDomain} = useRootLoaderData();
const className = `header-menu-${viewport}`;

function closeAside(event: React.MouseEvent<HTMLAnchorElement>) {
Expand Down
Loading
Loading