Score:-2

Generate static files of specific nodes with Tome

cg flag

I'm looking for a way to generate a static version of specified nodes ( in my case they are book nodes pages).

I want to do it periodically in my custom Drupal cronjob,

most of the Drupal static modules are obsolete and there is no version for Drupal 9.

I found TOME module but looks like it's mostly for generating full website static generation through admin panel or drush command line.

I tried drush tome:static-export-path /mybookpath --uri=localhost:8080 but it's blindly going through all the links on the pages and download all of them.

with digging more into the documentation I found this which is good to use tome services. in this case, there is no URI parameter option( it's only available for drush command) and it does not replace URLs ( image,link, style ( css) urls) and use the main version.

    <?php

/** @var \Drupal\tome_static\StaticGeneratorInterface $static */
$static = \Drupal::service('tome_static.generator');

/** @var \Drupal\tome_static\RequestPreparer $request_preparer */
$request_preparer = \Drupal::service('tome_static.request_preparer');

// Prepare for a new request. This is necessary so that static caches in Drupal
// can be reset between multiple requests in one bootstrap.
$request_preparer->prepareForRequest();

// Request an arbitrary path. "$invoke_paths" at this point is anything that
// needs to be copied (an existing file) or requested (an image derivative,
// other paths, etc.)
$invoke_paths = $static->requestPath('/foo');

// Export paths will try to copy or remove as many paths as possible without
// making a new internal request.
$invoke_paths = $static->exportPaths($invoke_paths);

// The remaining invoke paths need to be handled in new requests.
foreach ($invoke_paths as $path) {
  // Since we are only trying to export one path, restrict new requests to
  // paths with extensions, which are probably RSS feeds and image derivatives.
  if (pathinfo($path, PATHINFO_EXTENSION)) {
    $request_preparer->prepareForRequest();
    // This could return more paths, but we're ignoring that for simplicity.
    $static->requestPath($path);
  }
}

My question: What is the best solution to generate static HTML files will all assets of specific Drupal nodes?

Score:0
au flag

You can always recreate static pages with wget.

wget --mirror --convert-links --show-progress --content-disposition http://example.edu

This will create a copy of the entire site. I believe you can adjust the URL to be a specific node.

Yuseferi avatar
cg flag
thanks for your attention. I tried it but it goes through all the links on the page.
Yuseferi avatar
cg flag
I'm looking for more drupal base solution but ok .I can wrapp it in drupal command.
Score:0
cg flag

I used Tome and with the following function I implement custom path fetching

public static function storeFileLocally($uri) {
$base_url = "http://localhost:8080";
$path = $uri;
/** @var \Drupal\tome_static\StaticGeneratorInterface $static */
$static = \Drupal::service('tome_static.generator');

/** @var \Drupal\tome_static\RequestPreparer $request_preparer */
$request_preparer = \Drupal::service('tome_static.request_preparer');

TomeStaticHelper::setBaseUrl(\Drupal::request(), $base_url);

$request_preparer->prepareForRequest();
$invoke_paths = $static->requestPath($path);

$invoke_paths = $static->exportPaths($invoke_paths);

foreach ($invoke_paths as $path) {
  if (pathinfo($path, PATHINFO_EXTENSION)) {
    $request_preparer->prepareForRequest();
    $static->requestPath($path);
  }
}

}

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.