I am working on a Node.js application that features a homepage which loads content through an API dynamically. To optimize performance, I want to avoid fetching data every time a user visits the URL. Therefore, my goal is to generate a static HTML file containing the rendered content and save it in a specific folder for future access. Initially, I need to use a headless browser to visit the URL and extract the HTML content, storing it as a unique file (like File123.html) in my directory using Node.js. Below are my attempts to capture the dynamic HTML content:
Attempt 1:
const http = require('http');
http.get('http://localhost:3001/File123', function(response) {
response.setEncoding('utf8');
response.on('data', function(data) {
console.log(data);
});
});
Attempt 2:
const puppeteer = require('puppeteer');
(async () => {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('http://localhost:3001/File123');
await page.waitForSelector('html', { timeout: 3000 });
const content = await page.evaluate(() => {
return document.body.innerHTML;
});
console.log(content);
await browser.close();
} catch (err) {
console.error(err);
}
})();
Unfortunately, these methods only yield static HTML instead of the expected dynamic content. Below is the structure of HTML I typically receive:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="author" content="">
....
</head>
<body>
<div class='wrapper'>
<div class="container">
Loading...
<!-- DYNAMIC CONTENT -->
</div>
</div>
</body>
</html>
Could you provide guidance on how to improve my method?