scottie1984 / swagger-ui-express

Adds middleware to your express app to serve the Swagger UI bound to your Swagger document. This acts as living documentation for your API hosted from within your app.
MIT License
1.42k stars 225 forks source link

swagger-ui-init.js ignores cache-control option #262

Open suau opened 3 years ago

suau commented 3 years ago

I just struggled with disabling cache-control, serveWithOptions ignores the cacheControl option for swagger-ui-init.js. But it works for all other static files.

app.use("/api/v1/docs",
    swaggerUi.serveWithOptions({
        cacheControl: false
    }),
    async (_req: ExRequest, res: ExResponse) => {
        res.setHeader('Cache-Control', 'no-store, max-age=0');
        return res.send(
            swaggerUi.generateHTML(await import("../build/swagger.json"))
        );
    });

As a workaround I added another handler to run before serveWithOptions

    (_req: ExRequest, res: ExResponse, next: NextFunction) => {
        res.setHeader('Cache-Control', 'no-store, max-age=0');
        next();
    },
scottie1984 commented 2 years ago

swagger-ui-init.js is generated at runtime which is what the options aren't send through. I have looked and couldn't see any easy way to set the options when using res.send.

The only way I can think is the workaround you have implemented or writing a function to convert the options to headers. I could initially do it for the cacheControl and then implement further options as issues are raised.