#Configure Content Type headers in Astro

6 messages · Page 1 of 1 (latest)

sour viper
#

Hello! I need help configuring deep linking in my Astro webapp using the Node adapter.

Current setup:

  • Files are in public/.well-known/:
    • apple-app-site-association
    • assetlinks.json
  • Using Astro Node adapter
  • Files are accessible in the browser

Issue:
The deep linking isn't working because the files need to be served with the Content-Type: "application/json" header(check the screenshot), but I'm not sure how to configure this with Astro's Node adapter.

Questions:

  1. What's the correct way to serve these files with the proper Content-Type header in Astro?
  2. Should I be using middleware, or is there a built-in way to handle this?
  3. Are there any specific Astro configuration settings I need to modify?

Any guidance would be greatly appreciated! Thanks!

thorny moss
#

@formal fiber is our routing wizard.

formal fiber
#

This routing is not done by Astro itself actually. Each adapter does it's wiring on the platform. The Node adapter I believe just serves the content as a middleware. If you want finer control you can use middleware mode so Astro will be built to a middleware and then you can add a separate middleware with any customization you want in front of it

sour viper
#

Thanks so much!
I used the express adapter and it worked! Here's my code for the adapter:

// server.mjs
import express from "express";
import { handler as astroHandler } from "./dist/server/entry.mjs";

const app = express();

// Deep linking files with correct Content-Type
app.use(
  "/.well-known",
  express.static("./public/.well-known", {
    setHeaders: (res, filePath) => {
      if (
        filePath.endsWith("apple-app-site-association") ||
        filePath.endsWith("assetlinks.json")
      ) {
        res.set("Content-Type", "application/json");
      }
    },
  })
);

// Static files
app.use(express.static("./dist/client"));

// Astro handler
app.use(astroHandler);

const port = process.env.PORT || 4321;
app.listen(port, () => {
  console.log(`Server running on http://localhost:${port}`);
});
formal fiber
#

I guess we could add mime guessing through the extension and sniffing like I believe Vercel does