Skip to content

Commit

Permalink
Switch to different HTTP library for more reliable fetching.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmacarthur committed Sep 13, 2022
1 parent 8dac5b7 commit 5901ac5
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 153 deletions.
137 changes: 43 additions & 94 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
{
"name": "vite-plugin-proxy-page",
"version": "0.0.1",
"version": "0.0.2",
"license": "MIT",
"author": "Alex MacArthur (https://macarthur.me)",
"homepage": "https://github.com/alexmacarthur/vite-plugin-proxy-page",
"homepage": "https://macarthur.me/posts/project-local-spa-onto-production-page",
"scripts": {
"dev": "vite",
"test": "vitest run",
"test:watch": "vitest",
"build": "vite build && tsc",
"build": "rm -rf dist && npm run build:esm && npm run build:cjs",
"build:esm": "tsc --module es2015 --outDir dist/esm",
"build:cjs": "tsc --module commonjs --outDir dist/cjs",
"format": "prettier --write \"./**/*.{md,js,ts}\""
},
"main": "dist/index.umd.js",
"module": "dist/index.es.js",
"types": "dist/index.d.ts",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/types/index.d.ts",
"keywords": [
"vite-plugin",
"proxy",
Expand All @@ -28,7 +30,7 @@
"vitest": "^0.23.2"
},
"dependencies": {
"isomorphic-fetch": "^3.0.0",
"axios": "^0.27.2",
"jsdom": "^20.0.0"
}
}
21 changes: 13 additions & 8 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { proxyPage, htmlCache } from "./index";
import axios from "axios";

const url = "https://vite-proxy-demo.netlify.app/some-page";

Expand All @@ -10,27 +11,31 @@ beforeEach(() => {

describe("caching", () => {
it("performs fresh transformation when cache is empty", async () => {
const fetchSpy = vi.spyOn(globalThis, "fetch");
const requestSpy = vi.spyOn(axios, "get");

await proxyPage({
const plugin = proxyPage({
localEntryPoint: "local-dev.tsx",
remoteUrl: url,
}).transformIndexHtml();
});

expect(fetchSpy).toHaveBeenCalledTimes(1);
await plugin.transformIndexHtml("");

expect(requestSpy).toHaveBeenCalledTimes(1);
expect(htmlCache.get(url)).not.toBe(undefined);
});

it("uses cache when it's filled", async () => {
const fetchSpy = vi.spyOn(globalThis, "fetch");
const requestSpy = vi.spyOn(axios, "get");

htmlCache.set(url, "<html></html>");

await proxyPage({
const plugin = proxyPage({
localEntryPoint: "local-dev.tsx",
remoteUrl: url,
}).transformIndexHtml();
});

await plugin.transformIndexHtml("");

expect(fetchSpy).toHaveBeenCalledTimes(0);
expect(requestSpy).toHaveBeenCalledTimes(0);
});
});
Loading

0 comments on commit 5901ac5

Please sign in to comment.