3 min read
How to fake local domain?

Overview

Sometimes you need to fake domain on your local dev server. There are various of reasons to do it.

One of them is the situation when your deployed backend can receive requests only from specific domain.
But you need to test your frontend before deploying. Instead of changing security rules on your backend, you can fake local domain to production one. Let’s see how to do it.

Solution for Vite

  1. In the vite.config.ts file, add the following code:
export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
  server: {
    host: "fakedomain.com", // <---- here is your domain
  },
});

But if you try to run the dev server now, it will not work. You need to edit your hosts file.

  1. Add into your hosts file 127.0.0.1 fakedomain.com.
    If you are a Mac user, you can find it in /etc/hosts. img.png

Of course, instead of fakedomain.com, you need to add your domain which you want to fake.

  1. That’s it, now if you try to run server with npm run dev, it will work. img_1.png

Sometimes you need to fake domain on your local dev server. There are various of reasons to do it.

One of them is the situation when your deployed backend can receive requests only from specific domain.
But you need to test your frontend before deploying. Instead of changing security rules on your backend, you can fake local domain to production one. Let’s see how to do it.

Bonus level

You can also use mkcert from vite-plugin-mkcert plugin to fake https connection.

  1. Install dependency:
npm install -D vite-plugin-mkcert
  1. Add dependency to your vite.config.ts:
import path from "path";

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";
import mkcert from "vite-plugin-mkcert";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), mkcert()], // <---- add the plugin
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
  server: {
    host: "editor.crev.ai",
  },
});
  1. That’s it, now if you try to run server with npm run dev, it will run the server with https.