diff --git a/.changeset/lemon-trams-mix.md b/.changeset/lemon-trams-mix.md new file mode 100644 index 000000000..a4ed0281e --- /dev/null +++ b/.changeset/lemon-trams-mix.md @@ -0,0 +1,9 @@ +--- +'@asgardeo/browser': patch +'@asgardeo/nuxt': patch +--- + +Fix SSR compatibility and module bundling issues + +- **Browser:** Fixed Node.js ESM import resolution by changing directory imports (`'buffer/'`) to explicit file paths (`'buffer/index.js'`) +- **Nuxt:** Fixed Rollup bundling by centralizing module augmentations in `module.ts` and removing `.d.ts` file from dist diff --git a/packages/browser/esbuild.config.mjs b/packages/browser/esbuild.config.mjs index 90d81e359..900af451d 100644 --- a/packages/browser/esbuild.config.mjs +++ b/packages/browser/esbuild.config.mjs @@ -40,7 +40,7 @@ const polyfillPlugin = { // Buffer polyfill build.onResolve({filter: /^buffer$/}, () => ({ - path: require.resolve('buffer/'), + path: require.resolve('buffer/index.js'), })); }, }; @@ -48,7 +48,7 @@ const polyfillPlugin = { const commonOptions = { banner: { js: ` - import { Buffer } from 'buffer/'; + import { Buffer } from 'buffer/index.js'; if (typeof window !== 'undefined' && !window.Buffer) { window.Buffer = Buffer; } @@ -66,7 +66,7 @@ const commonOptions = { footer: { js: ` if (typeof window !== 'undefined' && !window.Buffer) { - window.Buffer = require('buffer/').Buffer; + window.Buffer = require('buffer/index.js').Buffer; } `, }, diff --git a/packages/nuxt/.npmignore b/packages/nuxt/.npmignore deleted file mode 100644 index 2127c205a..000000000 --- a/packages/nuxt/.npmignore +++ /dev/null @@ -1,2 +0,0 @@ -# Ignore TypeScript augmentation files from being processed by consuming apps' build tools -dist/runtime/types/augments.d.ts diff --git a/packages/nuxt/src/module.ts b/packages/nuxt/src/module.ts index cc669619a..4b0762be2 100644 --- a/packages/nuxt/src/module.ts +++ b/packages/nuxt/src/module.ts @@ -28,7 +28,7 @@ import { } from '@nuxt/kit'; import type {Nuxt} from '@nuxt/schema'; import {defu} from 'defu'; -import type {AsgardeoNuxtConfig} from './runtime/types'; +import type {AsgardeoNuxtConfig, AsgardeoSessionPayload, AsgardeoSSRData} from './runtime/types'; const PACKAGE_NAME: string = '@asgardeo/nuxt'; @@ -298,6 +298,12 @@ export default defineNuxtModule({ }); declare module '@nuxt/schema' { + interface NuxtConfig { + asgardeo?: AsgardeoNuxtConfig; + } + interface NuxtOptions { + asgardeo?: AsgardeoNuxtConfig; + } interface PublicRuntimeConfig { asgardeo: { afterSignInUrl: string; @@ -305,7 +311,7 @@ declare module '@nuxt/schema' { applicationId?: string; baseUrl: string; clientId: string; - preferences?: import('./runtime/types').AsgardeoNuxtConfig['preferences']; + preferences?: AsgardeoNuxtConfig['preferences']; scopes: string[]; signInUrl?: string; signUpUrl?: string; @@ -319,3 +325,13 @@ declare module '@nuxt/schema' { }; } } + +declare module 'h3' { + interface H3EventContext { + asgardeo?: { + isSignedIn?: boolean; + session?: AsgardeoSessionPayload | {sub?: string} | null; + ssr?: AsgardeoSSRData; + }; + } +} diff --git a/packages/nuxt/src/runtime/plugins/asgardeo.ts b/packages/nuxt/src/runtime/plugins/asgardeo.ts index 644bc63dd..af6eb31bb 100644 --- a/packages/nuxt/src/runtime/plugins/asgardeo.ts +++ b/packages/nuxt/src/runtime/plugins/asgardeo.ts @@ -27,9 +27,6 @@ import type {AsgardeoAuthState, AsgardeoSSRData} from '../types'; import {defineNuxtPlugin, useState, useRequestEvent, useRuntimeConfig, navigateTo} from '#app'; import type {NuxtApp} from '#app'; -// Import H3 augmentation so event.context.asgardeo is typed -import '../types/augments.d'; - /** * Universal Nuxt plugin (runs on both server and client) that wires up the * Asgardeo Vue SDK. @@ -122,7 +119,9 @@ export default defineNuxtPlugin((nuxtApp: NuxtApp) => { brandingState.value = ssr.brandingPreference; } else { // Backwards-compat: fall back to the legacy context shape (pre-Step-2 plugin). - const ssrContext: {isSignedIn?: boolean; session?: {sub?: string}} | undefined = event?.context?.asgardeo; + const ssrContext: {isSignedIn?: boolean; session?: {sub?: string}} | undefined = event?.context?.asgardeo as + | {isSignedIn?: boolean; session?: {sub?: string}} + | undefined; if (ssrContext) { authState.value = { isLoading: false, diff --git a/packages/nuxt/src/runtime/server/plugins/asgardeo-ssr.ts b/packages/nuxt/src/runtime/server/plugins/asgardeo-ssr.ts index 28a5728af..eafbe8464 100644 --- a/packages/nuxt/src/runtime/server/plugins/asgardeo-ssr.ts +++ b/packages/nuxt/src/runtime/server/plugins/asgardeo-ssr.ts @@ -24,9 +24,6 @@ import AsgardeoNuxtClient from '../AsgardeoNuxtClient'; import {verifyAndRehydrateSession} from '../utils/serverSession'; import {useRuntimeConfig} from '#imports'; -// Import augmentation so event.context.asgardeo is typed -import '../../types/augments.d'; - const log: ReturnType = createLogger('asgardeo-ssr'); const CALLBACK_PATH: string = '/api/auth/callback'; diff --git a/packages/nuxt/src/runtime/types/augments.d.ts b/packages/nuxt/src/runtime/types/augments.d.ts deleted file mode 100644 index 833ba4a1a..000000000 --- a/packages/nuxt/src/runtime/types/augments.d.ts +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com). - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import type {AsgardeoSessionPayload, AsgardeoSSRData} from './types'; - -/** - * Nuxt schema augmentation — adds `asgardeo` to NuxtConfig / NuxtOptions - * and ensures the runtime config shapes are fully typed. - */ -declare module '@nuxt/schema' { - interface NuxtConfig { - asgardeo?: import('./types').AsgardeoNuxtConfig; - } - interface NuxtOptions { - asgardeo?: import('./types').AsgardeoNuxtConfig; - } -} - -/** - * H3 event context augmentation — provides a typed `event.context.asgardeo` - * property so server plugins and API routes no longer need `as any` casts. - * - * Set by the Nitro `asgardeo-ssr` plugin on every page request. - */ -declare module 'h3' { - interface H3EventContext { - /** - * Resolved auth state for the current request. - * Populated by the Nitro `asgardeo-ssr` plugin during SSR page requests. - * `null` when the request is an API route or when the session is absent/invalid. - */ - asgardeo?: { - isSignedIn: boolean; - session: AsgardeoSessionPayload | null; - /** - * Rich SSR payload populated by the Nitro `asgardeo-ssr` plugin. - * Contains the user, userProfile, organisations, and branding data - * resolved on the server and seeded into `useState` keys for - * zero-cost client hydration. - */ - ssr?: AsgardeoSSRData; - }; - } -} - -export {};