Base URL option in generate form doesn't work when running Drupal in a sub-directory

Created on 9 January 2022, over 3 years ago
Updated 17 July 2023, almost 2 years ago

Problem/Motivation

I am running my Tome Drupal site in a subfolder, so it's URL is http://localhost:8888/tome-blog/web and the HTML static files get put in http://localhost:8888/tome-blog/html.

I went to admin/config/tome/static/generate and entered 'http://localhost:8888/tome-blog/html/' as the base URL and generated the static site.

However, my site at http://localhost:8888/tome-blog/html/ doesn't show any CSS because all the URIs in the HTML look like this:

    <link rel="stylesheet" media="all" href="/sites/default/files/css/css_pu8n-9XqDcbjLssKWX6HTUj95K8nWYuNQZ1q3rfKyik.css" />

with the initial /.

I was expecting the base URL value to be prepended to all URIs in the generated HTML, or better still, defined with https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

1.0

Component

Tome Static

Created by

🇬🇧United Kingdom joachim

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇬🇧United Kingdom joachim

    I've got a quick hack working with an outbound path processor service:

    class SiteRootOutboundPathProcessor implements OutboundPathProcessorInterface {
    
      /**
       * {@inheritdoc}
       */
      public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
        if (!$request) {
          // WTF? Why is $request sometimes NULL?
          return $path;
        }
    
        if (!$request->attributes->has(StaticGeneratorInterface::REQUEST_KEY)) {
          return $path;
        }
    
    
        // Hardcoded for now - should get this value from the base URL option in the form.
        $options['base_url'] = 'http://localhost:8888/tome-blog/html/tome-blog/web';
    
        return $path;
      }
    
    }
    
  • 🇬🇧United Kingdom joachim

    This also isn't working when I try to generate to a domain.

    With my site at http://localhost:8888/joachim-blog/htdocs on my local, and generating to https://joachim-n.github.io/, I get:

    - the root of the generated at http://localhost:8888/joachim-blog/html/joachim-blog/htdocs/index.html
    - the CSS links in the generated HTML have URLs like this: href="/joachim-blog/htdocs/themes/custom/joachim_blog_theme/fonts/metropolis/Metropolis-Regular.woff2"

  • 🇬🇧United Kingdom joachim

    I've tried adding the SiteRootOutboundPathProcessor I posted in an earlier comment, and that doesn't actually fix it -- I don't seem to have pages for path aliases exported at all with that in place!

  • 🇬🇧United Kingdom joachim

    For entity paths, the problem happens deep in core's UrlGenerator.

    Tome's EntityPathSubscriber calls it in the

          $event->setPath(parse_url($url->toString(), PHP_URL_PATH));
    

    This has the base URL for the actual site:

        $base_url = $this->context->getBaseUrl();
    

    and so returns "/drupal-contrib/web" (I've switched to a different site to experiment since my last comment!)

    UrlGenerator then does this, prepending the base URL:

        if (!$absolute || !$host = $this->context->getHost()) {
          $url = $base_url . $path . $query . $fragment;
    

    Then tome's EntityPathSubscriber get this URL for the entity:

    > '/drupal-contrib/web/node/1'

Production build 0.71.5 2024