Skip to content

Commit

Permalink
Add Cache control header for cloudfare configs
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Nov 24, 2024
1 parent 5b882f3 commit 28fe650
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PageType } from "../../../../interfaces/IDetailView";
import * as FetchGet from "../../../../shared/fetch/fetch-get";
import { FileListCache } from "../../../../shared/filelist-cache";
import { RemoveCache } from "./remove-cache";
import { CacheControl } from "../../../../shared/fetch/cache-control.ts";

describe("RemoveCache function", () => {
beforeEach(() => {
Expand Down Expand Up @@ -33,7 +34,9 @@ describe("RemoveCache function", () => {
.mockImplementationOnce(() => Promise.resolve({ statusCode: 200, data: null }));
const parentFolder = "/parent";
RemoveCache(jest.fn(), parentFolder, "search", jest.fn(), jest.fn());
expect(mockFetchGet).toHaveBeenCalledWith("/starsky/api/remove-cache?json=true&f=/parent");
expect(mockFetchGet).toHaveBeenCalledWith("/starsky/api/remove-cache?json=true&f=/parent", {
CacheControl
});
expect(mockFetchGet.mock.calls[0][0]).toContain("/remove-cache");
expect(mockFetchGet.mock.calls[0][0]).toContain(
"/starsky/api/remove-cache?json=true&f=/parent"
Expand All @@ -50,7 +53,9 @@ describe("RemoveCache function", () => {
const parent: string | undefined = undefined as unknown as string;

RemoveCache(jest.fn(), parent, "search", jest.fn(), jest.fn());
expect(mockFetchGet).toHaveBeenCalledWith("/starsky/api/remove-cache?json=true&f=/");
expect(mockFetchGet).toHaveBeenCalledWith("/starsky/api/remove-cache?json=true&f=/", {
CacheControl
});
expect(mockFetchGet.mock.calls[0][0]).toContain("/remove-cache");
expect(mockFetchGet.mock.calls[0][0]).toContain("/starsky/api/remove-cache?json=true&f=/");
mockFetchGet.mockRestore();
Expand All @@ -67,7 +72,9 @@ describe("RemoveCache function", () => {

jest.runAllTimers();

expect(mockFetchGet).toHaveBeenCalledWith("/starsky/api/remove-cache?json=true&f=/parent");
expect(mockFetchGet).toHaveBeenCalledWith("/starsky/api/remove-cache?json=true&f=/parent", {
CacheControl
});

mockFetchGet.mockRestore();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import FetchGet from "../../../../shared/fetch/fetch-get.ts";
import { FileListCache } from "../../../../shared/filelist-cache.ts";
import { URLPath } from "../../../../shared/url/url-path.ts";
import { UrlQuery } from "../../../../shared/url/url-query.ts";
import { CacheControl } from "../../../../shared/fetch/cache-control.ts";

/**
* Remove Folder cache
Expand All @@ -20,7 +21,7 @@ export function RemoveCache(
setIsLoading(true);
new FileListCache().CacheCleanEverything();
const parentFolder = propsParentFolder ?? "/";
FetchGet(new UrlQuery().UrlRemoveCache(new URLPath().encodeURI(parentFolder)))
FetchGet(new UrlQuery().UrlRemoveCache(new URLPath().encodeURI(parentFolder)), { CacheControl })
.then(() => {
return new Promise<string>((resolve) => {
setTimeout(() => {
Expand All @@ -31,7 +32,7 @@ export function RemoveCache(
});
})
.then((url: string) => {
return FetchGet(url, { "Cache-Control": "no-store, max-age=0" });
return FetchGet(url, { CacheControl });
})
.then((connectionResult) => {
const removeCacheResult = new CastToInterface().MediaArchive(connectionResult.data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as FetchPost from "../../../shared/fetch/fetch-post";
import { UrlQuery } from "../../../shared/url/url-query";
import * as Modal from "../../atoms/modal/modal";
import ModalArchiveSynchronizeManually from "./modal-archive-synchronize-manually";
import { CacheControl } from "../../../shared/fetch/cache-control.ts";

describe("ModalArchiveSynchronizeManually", () => {
beforeEach(() => {
Expand Down Expand Up @@ -105,7 +106,9 @@ describe("ModalArchiveSynchronizeManually", () => {
});

expect(fetchGetSpy).toHaveBeenCalled();
expect(fetchGetSpy).toHaveBeenCalledWith(new UrlQuery().UrlRemoveCache("/"));
expect(fetchGetSpy).toHaveBeenCalledWith(new UrlQuery().UrlRemoveCache("/"), {
CacheControl
});

fetchGetSpy.mockReset();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { UrlQuery } from "../../../shared/url/url-query";
import FormControl from "../../atoms/form-control/form-control";
import Modal from "../../atoms/modal/modal";
import Select from "../../atoms/select/select";
import { CacheControl } from "../../../shared/fetch/cache-control.ts";

interface IModalPublishProps {
isOpen: boolean;
Expand Down Expand Up @@ -97,7 +98,7 @@ const ModalPublish: React.FunctionComponent<IModalPublishProps> = (props) => {
return;
}

FetchGet(new UrlQuery().UrlPublishExist(toUpdateItemName)).then((result) => {
FetchGet(new UrlQuery().UrlPublishExist(toUpdateItemName), { CacheControl }).then((result) => {
if (result.statusCode !== 200) return;
setExistItemName(result.data);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as FetchGet from "../fetch/fetch-get";
import { UrlQuery } from "../url/url-query";
import { ExportIntervalUpdate } from "./export-interval-update";
import { ProcessingState } from "./processing-state";
import { CacheControl } from "../fetch/cache-control.ts";

describe("ExportIntervalUpdate", () => {
it("ready", async () => {
Expand All @@ -20,7 +21,9 @@ describe("ExportIntervalUpdate", () => {
await ExportIntervalUpdate("test", setProcessingSpy);

expect(fetchGetSpy).toHaveBeenCalled();
expect(fetchGetSpy).toHaveBeenCalledWith(new UrlQuery().UrlExportZipApi("test", true));
expect(fetchGetSpy).toHaveBeenCalledWith(new UrlQuery().UrlExportZipApi("test", true), {
CacheControl
});
expect(setProcessingSpy).toHaveBeenCalled();
expect(setProcessingSpy).toHaveBeenCalledWith(ProcessingState.ready);
});
Expand All @@ -40,7 +43,9 @@ describe("ExportIntervalUpdate", () => {
await ExportIntervalUpdate("test", setProcessingSpy);

expect(fetchGetSpy).toHaveBeenCalled();
expect(fetchGetSpy).toHaveBeenCalledWith(new UrlQuery().UrlExportZipApi("test", true));
expect(fetchGetSpy).toHaveBeenCalledWith(new UrlQuery().UrlExportZipApi("test", true), {
CacheControl
});
expect(setProcessingSpy).toHaveBeenCalled();
expect(setProcessingSpy).toHaveBeenCalledWith(ProcessingState.fail);
});
Expand All @@ -60,7 +65,9 @@ describe("ExportIntervalUpdate", () => {
await ExportIntervalUpdate("test", setProcessingSpy);

expect(fetchGetSpy).toHaveBeenCalled();
expect(fetchGetSpy).toHaveBeenCalledWith(new UrlQuery().UrlExportZipApi("test", true));
expect(fetchGetSpy).toHaveBeenCalledWith(new UrlQuery().UrlExportZipApi("test", true), {
CacheControl
});
expect(setProcessingSpy).toHaveBeenCalledTimes(0);
});

Expand All @@ -79,7 +86,9 @@ describe("ExportIntervalUpdate", () => {
await ExportIntervalUpdate("test", setProcessingSpy);

expect(fetchGetSpy).toHaveBeenCalled();
expect(fetchGetSpy).toHaveBeenCalledWith(new UrlQuery().UrlExportZipApi("test", true));
expect(fetchGetSpy).toHaveBeenCalledWith(new UrlQuery().UrlExportZipApi("test", true), {
CacheControl
});
expect(setProcessingSpy).toHaveBeenCalledTimes(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { Dispatch } from "react";
import FetchGet from "../fetch/fetch-get";
import { UrlQuery } from "../url/url-query";
import { ProcessingState } from "./processing-state";
import { CacheControl } from "../fetch/cache-control.ts";

export async function ExportIntervalUpdate(
zipKey: string,
setProcessing: Dispatch<ProcessingState>
) {
// need to check if ProcessingState = server
if (!zipKey) return;
const result = await FetchGet(new UrlQuery().UrlExportZipApi(zipKey, true));
const result = await FetchGet(new UrlQuery().UrlExportZipApi(zipKey, true), { CacheControl });

switch (result.statusCode) {
case 200:
Expand Down
2 changes: 2 additions & 0 deletions starsky/starsky/clientapp/src/shared/fetch/cache-control.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const cacheControlValue = "no-store, max-age=0";
export const CacheControl = { "Cache-Control": cacheControlValue };

0 comments on commit 28fe650

Please sign in to comment.