Skip to content

Commit 28c1311

Browse files
committed
feat(sitemap): create test and get routes
1 parent 73f5ca4 commit 28c1311

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const defaultOptions = {
2727
cache: true
2828
},
2929
puppeteerArgs: [],
30+
generateSitemap: true,
3031
puppeteerExecutablePath: undefined,
3132
puppeteerIgnoreHTTPSErrors: false,
3233
publicPath: "/",
@@ -260,6 +261,26 @@ const removeBlobs = async opt => {
260261
});
261262
};
262263

264+
/**
265+
*
266+
* @param {{page: Page}} opt
267+
* @return Promise
268+
*/
269+
const generateSitemap = async opt => {
270+
const { page, pageUrl, indexRoutes } = opt;
271+
try{
272+
console.log('generate sitemap function')
273+
console.log('-start-')
274+
console.log(pageUrl)
275+
console.log(indexRoutes)
276+
indexRoutes.push(pageUrl)
277+
console.log(indexRoutes)
278+
console.log('-end-')
279+
} catch (e) {
280+
return Promise.reject(e.message);
281+
}
282+
};
283+
263284
/**
264285
* @param {{page: Page, pageUrl: string, options: {skipThirdPartyRequests: boolean, userAgent: string}, basePath: string, browser: Browser}} opt
265286
* @return {Promise}
@@ -695,6 +716,7 @@ const run = async (userOptions, { fs } = { fs: nativeFs }) => {
695716
const ajaxCache = {};
696717
const { http2PushManifest } = options;
697718
const http2PushManifestItems = {};
719+
const indexRoutes = [];
698720

699721
await crawl({
700722
options,
@@ -730,9 +752,11 @@ const run = async (userOptions, { fs } = { fs: nativeFs }) => {
730752
},
731753
afterFetch: async ({ page, route, browser, addToQueue }) => {
732754
const pageUrl = `${basePath}${route}`;
755+
await console.log('--AFTER FETCH--')
733756
if (options.removeStyleTags) await removeStyleTags({ page });
734757
if (options.removeScriptTags) await removeScriptTags({ page });
735758
if (options.removeBlobs) await removeBlobs({ page });
759+
if (options.generateSitemap) await generateSitemap({ page, pageUrl, indexRoutes });
736760
if (options.inlineCss) {
737761
const { cssFiles } = await inlineCss({
738762
page,
@@ -876,6 +900,14 @@ const run = async (userOptions, { fs } = { fs: nativeFs }) => {
876900
JSON.stringify(manifest)
877901
);
878902
}
903+
if (generateSitemap) {
904+
console.log("@@--ON FILE END SITEMAP--@@")
905+
console.log("---")
906+
head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?\>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"
907+
tail = "</urlset>"
908+
console.log(indexRoutes)
909+
console.log("@@@@")
910+
}
879911
}
880912
});
881913
};

tests/generateSitemap.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// FIX: tests are slow - use unit tests instead of integration tests
2+
// TODO: capture console log from run function
3+
const fs = require("fs");
4+
const writeFileSpy = jest.spyOn(fs, "writeFile");
5+
writeFileSpy.mockImplementation((file, data, cb) => cb());
6+
7+
const { mockFs } = require("./helper.js");
8+
const { run } = require("./../index.js");
9+
const snapRun = (fs, options) =>
10+
run(
11+
{
12+
// for Travis CI
13+
puppeteerArgs: ["--no-sandbox", "--disable-setuid-sandbox"],
14+
// sometimes web server from previous test have not enough time to shut down
15+
// as a result you get `Error: listen EADDRINUSE :::45678`
16+
// to prevent this we use random port
17+
port: Math.floor(Math.random() * 1000 + 45000),
18+
...options
19+
},
20+
{
21+
fs
22+
}
23+
);
24+
25+
describe("generateSitemap", () => {
26+
console.log('\n--describing generate sitemap test--\n')
27+
const source = "tests/examples/many-pages";
28+
const include = ["/index.html"];
29+
const { fs, filesCreated, content } = mockFs();
30+
beforeAll(() => snapRun(fs, { source, include }));
31+
test("removes blob resources from final html but sitemap", () => {
32+
console.log('--fs')
33+
console.log(fs)
34+
console.log('--content(0)')
35+
console.log(content(0))
36+
expect(content(0)).not.toMatch('<link rel="stylesheet" href="blob:');
37+
});
38+
});
39+
40+
describe.skip("publicPath", () => {});
41+
42+
describe.skip("skipThirdPartyRequests", () => {});
43+
44+
describe.skip("waitFor", () => {});
45+
46+
describe.skip("externalServer", () => {});

0 commit comments

Comments
 (0)