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

Auth middleware invoked on 404 Errors for static assets in _nuxt folder #989

Open
martinsbuss opened this issue Jan 22, 2025 · 0 comments
Open
Labels
bug A bug that needs to be resolved pending An issue waiting for triage

Comments

@martinsbuss
Copy link

Environment

  • Operating System: Darwin
  • Node Version: v20.18.1
  • Nuxt Version: 3.15.2
  • CLI Version: 3.20.0
  • Nitro Version: 2.10.4
  • Package Manager: [email protected]
  • Builder: -
  • User Config: compatibilityDate, devtools, modules
  • Runtime Modules: @sidebase/[email protected]
  • Build Modules: -

Reproduction

nuxt.config.ts:

export default defineNuxtConfig({
  compatibilityDate: '2024-11-01',
  devtools: { enabled: true },
  modules: ['@sidebase/nuxt-auth'],
  auth: {
    baseURL: 'http://localhost:8000/api/',
    provider: {
      type: 'local',
      pages: {
        login: '/login',
      },
      endpoints: {
        signIn: { path: 'user/login', method: 'post' },
        signOut: { path: 'user/logout', method: 'post' },
        signUp: { path: 'user/register', method: 'post' },
        getSession: { path: 'user', method: 'get' },
      },
    }
  }
})
  1. Log in as any user.
  2. Open any static asset in the _nuxt folder (e.g., /_nuxt/foo-bar.js).
  3. If the asset doesn't exist on the server:
  • Nuxt returns a 404 error.
  • Before returning the error, it initializes the nuxt instance and calls the API endpoint to retrieve user data for each non-existent file.

Describe the bug

When Nuxt accesses static assets in the _nuxt folder:

  • If the file exists, Nuxt serves its content as expected.
  • If the file does not exist (e.g., after deployment), Nuxt returns a 404 error but first:
    • Initializes an instance of the auth middleware.
    • Makes an API request to fetch authenticated user data, even though this is unnecessary for static assets.

This behavior can result in a significant number of redundant API calls when multiple missing files are requested (e.g., during deployment or cache misses). If 10 JavaScript files result in 404 errors (e.g., after deployment), this could trigger 10 API calls to the getSession endpoint specified in the Nuxt configuration.

Additional context

A temporary workaround involves disabling server-side authentication for static routes in the Nuxt configuration.

Example Configuration:

export default defineNuxtConfig({
  compatibilityDate: '2024-11-01',
  devtools: { enabled: true },
  modules: ['@sidebase/nuxt-auth'],
  nitro: {
    routeRules: {
      '/_nuxt/**': {
        auth: {
          disableServerSideAuth: true,
        },
      },
    },
  },  
  auth: {
    baseURL: 'http://localhost:8000/api/',
    provider: {
      type: 'local',
      pages: {
        login: '/login',
      },
      endpoints: {
        signIn: { path: 'user/login', method: 'post' },
        signOut: { path: 'user/logout', method: 'post' },
        signUp: { path: 'user/register', method: 'post' },
        getSession: { path: 'user', method: 'get' },
      },
    },
  },
});

Logs

@martinsbuss martinsbuss added bug A bug that needs to be resolved pending An issue waiting for triage labels Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug that needs to be resolved pending An issue waiting for triage
Projects
None yet
Development

No branches or pull requests

1 participant