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

feat: auto url on multistore #7244

Merged
merged 5 commits into from
Aug 27, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/itchy-cycles-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vue-storefront/nuxt": major
---

[BREAKING]: Added functionality that automatically configures the proper URLs for SSR & SPA modes when mutlistore mode is enabled. Left your environment variables like in normal mode, just set `NUXT_PUBLIC_ALOKAI_MULTISTORE_ENABLED` to `true`.
22 changes: 22 additions & 0 deletions .changeset/selfish-knives-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
"@vue-storefront/next": minor
---

[CHANGE]: Added the `resolveSdkOptions` helper that automatically setup proper SSR & SPA URLs for middleware when multistore option is enabled. To use it, change your SDK configuration:

```diff
-import type { CreateSdkOptions } from '@vue-storefront/next';
+import { resolveSdkOptions } from '@vue-storefront/next';

-const options: CreateSdkOptions = {
+const options = resolveSdkOptions({
middleware: {
apiUrl,
cdnCacheBustingId,
ssrApiUrl,
},
multistore: {
enabled: isMultiStoreEnabled,
},
});
```
8 changes: 4 additions & 4 deletions docs/content/4.sdk/2.getting-started/1.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ If you want to keep your Storefront more configurable, we highly recommend using
::

```ts [sdk/options.ts]
import type { CreateSdkOptions } from '@vue-storefront/next';
import { resolveSdkOptions } from '@vue-storefront/next';
import { env } from 'next-runtime-env';

export function getSdkOptions() {
const apiUrl = env('NEXT_PUBLIC_ALOKAI_MIDDLEWARE_API_URL') ?? '';
const ssrApiUrl = env('NEXT_PUBLIC_ALOKAI_MIDDLEWARE_SSR_API_URL');
const cdnCacheBustingId = env('NEXT_PUBLIC_ALOKAI_MIDDLEWARE_CDN_CACHE_BUSTING_ID') ?? 'no-cache-busting-id-set';
const isMultiStoreEnabled = env('NEXT_PUBLIC_MULTISTORE_ENABLED') === 'true';
const isMultiStoreEnabled = env('NEXT_PUBLIC_ALOKAI_MULTISTORE_ENABLED') === 'true';
if (!apiUrl) {
throw new Error('NEXT_PUBLIC_ALOKAI_MIDDLEWARE_API_URL is required to run the app');
}

const options: CreateSdkOptions = {
const options = resolveSdkOptions({
middleware: {
apiUrl,
cdnCacheBustingId,
Expand All @@ -61,7 +61,7 @@ export function getSdkOptions() {
multistore: {
enabled: isMultiStoreEnabled,
},
};
});

return options;
}
Expand Down
32 changes: 32 additions & 0 deletions packages/next/src/sdk/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,35 @@ export function defineSdkConfig<TConfig extends Record<string, any>>(
) {
return config;
}

type ResolveSdkOptionsConfig = {
customSuffix: string;
};

/**
* Helper function to resolve the SDK options based on the configuration.
* @param input - The options for creating the SDK.
* @param options - The configuration object, that allows to customize the API Url creation on enabled multistore.
* @returns The resolved SDK options.
*/
export function resolveSdkOptions(
input: CreateSdkOptions,
options: Partial<ResolveSdkOptionsConfig> = {}
): CreateSdkOptions {
if (input?.multistore?.enabled) {
return {
middleware: {
// This is a dummy URL, the localhost domain will be replaced by the actual domain in the browser
// in composeMiddlewareUrl function. The server-side rendering will use ssrApiUrl.
apiUrl: `https://localhost/${options?.customSuffix ?? "api"}`,
cdnCacheBustingId: input.middleware?.cdnCacheBustingId,
ssrApiUrl: input.middleware?.ssrApiUrl ?? input.middleware.apiUrl,
},
multistore: {
enabled: true,
},
};
}

return input;
}
30 changes: 29 additions & 1 deletion packages/nuxt/src/runtime/defineSdkConfig.template
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ export function defineSdkConfig<TConfig>(config: Config<TConfig>) {
const runtimeConfig = useRuntimeConfig();

const requestHeaders = useRequestHeaders(["x-forwarded-host", "host"]);
const resolvedOptions = resolveOptions(runtimeConfig.public.alokai);

const middlewareUrl = composeMiddlewareUrl({
options: runtimeConfig.public.alokai,
options: resolvedOptions,
headers: requestHeaders,
});

Expand All @@ -89,3 +91,29 @@ export function defineSdkConfig<TConfig>(config: Config<TConfig>) {
});
}
}

function resolveSdkOptions(
input: SdkModuleOptions,
options: Partial<ResolveSdkOptionsConfig> = {}
): SdkModuleOptions {
if (input?.multistore?.enabled) {
return {
middleware: {
// This is a dummy URL, the localhost domain will be replaced by the actual domain in the browser
// in composeMiddlewareUrl function. The server-side rendering will use ssrApiUrl.
apiUrl: `https://localhost/${options?.customSuffix ?? "api"}`,
Fifciu marked this conversation as resolved.
Show resolved Hide resolved
cdnCacheBustingId: input.middleware?.cdnCacheBustingId,
ssrApiUrl: input.middleware?.ssrApiUrl ?? input.middleware.apiUrl,
},
multistore: {
enabled: true,
},`
};
}

return input;
}

type ResolveSdkOptionsConfig = {
customSuffix: string;
}
2 changes: 2 additions & 0 deletions packages/nuxt/src/runtime/utils/composeMiddlewareUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ function calculateMiddlewareUrl({

if (typeof window !== "undefined") {
if (options.multistore?.enabled) {
// In options the apiUrl is a dummy URL with the localhost domain and real subpath.
// Here we replacing by the actual domain from the browser.
const url = new URL(apiUrl);
url.host = window.location.host;
return url.href;
Expand Down
Loading