Skip to content

Commit 49dbecf

Browse files
committed
feat(pat ajax): Allow caching of ajax requests by using the option cache which can be set to caching or no-caching.
1 parent 0ca905e commit 49dbecf

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

src/pat/ajax/ajax.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ parser.addArgument("url", function ($el) {
1818
$el.is("a") ? $el.attr("href") : $el.is("form") ? $el.attr("action") : ""
1919
).split("#")[0];
2020
});
21-
22-
$.ajaxSetup({
23-
// Disable caching of AJAX responses
24-
cache: false,
25-
});
21+
parser.addArgument("browser-cache", "no-cache", ["cache", "no-cache"]); // Cache ajax requests
2622

2723
const xhrCount = {};
2824
xhrCount.get = function (a) {
@@ -104,6 +100,7 @@ const _ = {
104100
headers: {},
105101
url: cfg.url,
106102
method: $el.attr("method") ? $el.attr("method") : "GET",
103+
cache: cfg.browserCache === "cache" ? true : false,
107104
};
108105

109106
if (cfg.accept) {

src/pat/ajax/ajax.test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,62 @@ describe("pat-ajax", function () {
154154
expect(ajaxargs.headers).toEqual({ Accept: "*/*" });
155155
});
156156
});
157+
158+
describe("caching", () => {
159+
afterEach(function () {
160+
document.body.innerHTML = "";
161+
});
162+
163+
it("does not cache by default", function () {
164+
const spy_ajax = jest.spyOn($, "ajax");
165+
document.body.innerHTML = `<a
166+
class="pat-ajax"
167+
/>`;
168+
registry.scan(document.body);
169+
document.body.querySelector(".pat-ajax").click();
170+
const ajaxargs = spy_ajax.mock.calls[spy_ajax.mock.calls.length - 1][0];
171+
expect(ajaxargs.cache).toBe(false);
172+
spy_ajax.mockRestore();
173+
});
174+
175+
it("does not cache when explicitly set", function () {
176+
const spy_ajax = jest.spyOn($, "ajax");
177+
document.body.innerHTML = `<a
178+
class="pat-ajax"
179+
data-pat-ajax="browser-cache: no-cache"
180+
/>`;
181+
registry.scan(document.body);
182+
document.body.querySelector(".pat-ajax").click();
183+
const ajaxargs = spy_ajax.mock.calls[spy_ajax.mock.calls.length - 1][0];
184+
expect(ajaxargs.cache).toBe(false);
185+
spy_ajax.mockRestore();
186+
});
187+
188+
it("does cache when explicitly set", function () {
189+
const spy_ajax = jest.spyOn($, "ajax");
190+
document.body.innerHTML = `<a
191+
class="pat-ajax"
192+
data-pat-ajax="browser-cache: cache"
193+
/>`;
194+
registry.scan(document.body);
195+
document.body.querySelector(".pat-ajax").click();
196+
const ajaxargs = spy_ajax.mock.calls[spy_ajax.mock.calls.length - 1][0];
197+
expect(ajaxargs.cache).toBe(true);
198+
spy_ajax.mockRestore();
199+
});
200+
201+
it("does not cache on POST forms, regardless of the setting", function () {
202+
const spy_ajax = jest.spyOn($, "ajax");
203+
document.body.innerHTML = `<form
204+
class="pat-ajax"
205+
method="POST"
206+
data-pat-ajax="browser-cache: cache"
207+
/>`;
208+
registry.scan(document.body);
209+
$(".pat-ajax").submit(); // need jquery submit here
210+
const ajaxargs = spy_ajax.mock.calls[spy_ajax.mock.calls.length - 1][0];
211+
expect(ajaxargs.cache).toBe(false);
212+
spy_ajax.mockRestore();
213+
});
214+
});
157215
});

0 commit comments

Comments
 (0)