statamic / ssg

The official Statamic Static Site Generator
233 stars 24 forks source link

Call to a member function absoluteUrl() on null #60

Closed tao closed 3 years ago

tao commented 3 years ago

It seems like since updating I'm having an issue with this absoluteUrl method:

Gathering content to be generated...

   Error

  Call to a member function absoluteUrl() on null

  at vendor/statamic/cms/src/Routing/Routable.php:59
     55▕
     56▕     public function absoluteUrlWithoutRedirect()
     57▕     {
     58▕         $url = vsprintf('%s/%s', [
  ➜  59▕             rtrim($this->site()->absoluteUrl() ?? '/', '/'),
     60▕             ltrim($this->uri(), '/'),
     61▕         ]);
     62▕
     63▕         return $url === '/' ? $url : rtrim($url, '/');

      +5 vendor frames
  6   [internal]:0
      Illuminate\Support\Collection::Illuminate\Support\Traits\{closure}(Object(Statamic\StaticSite\Page))

      +23 vendor frames
  30  please:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

If I do a quick modification to the function to test $this->site() is not null before calling ->absoluteUrl() then it seems to work not throw this error anymore.

    public function absoluteUrlWithoutRedirect()
    {
        $absoluteUrl = '/';
        if ($this->site()) { $absoluteUrl = $this->site()->absoluteUrl(); }

        $url = vsprintf('%s/%s', [
            rtrim($absoluteUrl, '/'),
            ltrim($this->uri(), '/'),
        ]);

        return $url === '/' ? $url : rtrim($url, '/');
    }

But after over-coming the first one I'm also getting this error on random pages during generation... so $site is null is causing a larger issue in other classes too?

   Error

  Call to a member function handle() on null

  at vendor/statamic/ssg/src/Generator.php:355
    351▕     }
    352▕
    353▕     protected function updateCurrentSite($site)
    354▕     {
  ➜ 355▕         Site::setCurrent($site->handle());
    356▕         Cascade::withSite($site);
    357▕
    358▕         // Set the locale for dates, carbon, and for the translator.
    359▕         // This is what happens in Statamic's Localize middleware.

      +22 vendor frames
  23  please:37

As well as this one at the end:

[✔] Generated 2105 content files

   TypeError

  Cannot access offset of type string on string

  at vendor/statamic/ssg/src/Generator.php:270
    266▕         Partyline::line("\x1B[1A\x1B[2K[✔] Generated {$results->sum('count')} content files");
    267▕
    268▕         if ($results->sum('skips')) {
    269▕             $results->reduce(function ($carry, $item) {
  ➜ 270▕                 return $carry->merge($item['errors']);
    271▕             }, collect())->each(function ($error) {
    272▕                 Partyline::line($error);
    273▕             });
    274▕         }

      +18 vendor frames
  19  please:37
      Illuminate\Foundation\Console\Kernel::handle()
tao commented 3 years ago

The error was caused by a config error as there was a mismatch between the sites handle in the config and the languages handle used in the collection yaml file.

thomaskenne commented 3 years ago

Hi @tao

I have the same problem. Where, more specific, did you have the mismatch? Thanks.

tao commented 3 years ago

@thomaskenne this was a while ago so can't remember 100% but double check that the sites handle in config/statamic/sites.php is that same as the ones used in your collection yaml file and the folder names in each collection are the same too.

For example, if your sites config has this:

    'sites' => [
        'en' => [
            'name' => 'English',
            'locale' => 'en_US',
            'url' => '/',
        ],
        'bg' => [
            'name' => 'Bulgarian',
            'locale' => 'bg_BG',
            'url' => '/bg/',
        ],

Make sure the collection yaml file has the same ones:

title: Articles
sites:
  - en
  - bg (not bs)

As well as the folders in Articles subfolders should match:

├── articles
│   └── en
│   └── bg
thomaskenne commented 3 years ago

@tao thanks Daniel! Unfortunately this is not the case here. Seems to be at random where ssg halts when building. Hmm. I will dig deeper.

jasonvarga commented 3 years ago

The randomness is to be expected - the SSG generates the pages in a random order.

If you could provide the stack trace which ends up in your log file, that would be helpful.

thomaskenne commented 3 years ago

@jasonvarga ah - ok. Thanks for chiming in:

This the support details:

Statamic 3.2.6 Pro
Laravel 8.61.0
PHP 8.0.10
statamic/ssg 0.8.0

Here is a stacktrace:

[2021-09-24 14:19:19] local.ERROR: Call to a member function handle() on null {"exception":"[object] (Error(code: 0): Call to a member function handle() on null at /Users/thomas/Projects/doording-cms/vendor/statamic/ssg/src/Generator.php:353)
[stacktrace]
#0 /Users/thomas/Projects/doording-cms/vendor/statamic/ssg/src/Generator.php(232): Statamic\\StaticSite\\Generator->updateCurrentSite(NULL)
#1 /Users/thomas/Projects/doording-cms/vendor/spatie/fork/src/Task.php(92): Statamic\\StaticSite\\Generator->Statamic\\StaticSite\\{closure}()
#2 /Users/thomas/Projects/doording-cms/vendor/spatie/fork/src/Fork.php(154): Spatie\\Fork\\Task->execute()
#3 /Users/thomas/Projects/doording-cms/vendor/spatie/fork/src/Fork.php(128): Spatie\\Fork\\Fork->executeInChildTask(Object(Spatie\\Fork\\Task), Object(Spatie\\Fork\\Connection))
#4 /Users/thomas/Projects/doording-cms/vendor/spatie/fork/src/Fork.php(103): Spatie\\Fork\\Fork->forkForTask(Object(Spatie\\Fork\\Task))
#5 /Users/thomas/Projects/doording-cms/vendor/spatie/fork/src/Fork.php(183): Spatie\\Fork\\Fork->runTask(Object(Spatie\\Fork\\Task))
#6 /Users/thomas/Projects/doording-cms/vendor/spatie/fork/src/Fork.php(76): Spatie\\Fork\\Fork->startRunning(Object(Spatie\\Fork\\Task))
#7 /Users/thomas/Projects/doording-cms/vendor/spatie/fork/src/Fork.php(69): Spatie\\Fork\\Fork->waitFor(Object(Spatie\\Fork\\Task))
#8 /Users/thomas/Projects/doording-cms/vendor/statamic/ssg/src/ConcurrentTasks.php(18): Spatie\\Fork\\Fork->run(Object(Closure))
#9 /Users/thomas/Projects/doording-cms/vendor/statamic/ssg/src/Generator.php(185): Statamic\\StaticSite\\ConcurrentTasks->run(Object(Closure))
#10 /Users/thomas/Projects/doording-cms/vendor/statamic/ssg/src/Generator.php(91): Statamic\\StaticSite\\Generator->createContentFiles()
#11 /Users/thomas/Projects/doording-cms/vendor/statamic/ssg/src/Commands/StaticSiteGenerate.php(60): Statamic\\StaticSite\\Generator->generate()
#12 /Users/thomas/Projects/doording-cms/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Statamic\\StaticSite\\Commands\\StaticSiteGenerate->handle()
#13 /Users/thomas/Projects/doording-cms/vendor/laravel/framework/src/Illuminate/Container/Util.php(40): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#14 /Users/thomas/Projects/doording-cms/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\\Container\\Util::unwrapIfClosure(Object(Closure))
#15 /Users/thomas/Projects/doording-cms/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#16 /Users/thomas/Projects/doording-cms/vendor/laravel/framework/src/Illuminate/Container/Container.php(651): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#17 /Users/thomas/Projects/doording-cms/vendor/laravel/framework/src/Illuminate/Console/Command.php(136): Illuminate\\Container\\Container->call(Array)
#18 /Users/thomas/Projects/doording-cms/vendor/symfony/console/Command/Command.php(299): Illuminate\\Console\\Command->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#19 /Users/thomas/Projects/doording-cms/vendor/laravel/framework/src/Illuminate/Console/Command.php(121): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#20 /Users/thomas/Projects/doording-cms/vendor/symfony/console/Application.php(978): Illuminate\\Console\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#21 /Users/thomas/Projects/doording-cms/vendor/symfony/console/Application.php(295): Symfony\\Component\\Console\\Application->doRunCommand(Object(Statamic\\StaticSite\\Commands\\StaticSiteGenerate), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#22 /Users/thomas/Projects/doording-cms/vendor/symfony/console/Application.php(167): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#23 /Users/thomas/Projects/doording-cms/vendor/laravel/framework/src/Illuminate/Console/Application.php(92): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#24 /Users/thomas/Projects/doording-cms/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(129): Illuminate\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#25 /Users/thomas/Projects/doording-cms/please(37): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#26 {main}
jasonvarga commented 3 years ago

Right between these two lines: https://github.com/statamic/ssg/blob/54dd99f77db5620af44e6c84fa549a31c1f30bd9/src/Generator.php#L231-L232 (Which will be in vendor/statamic/ssg/src/Generator.php)

Add this:

if (!$page->site()) {
    dd($page->url(), $page);
}

Then run the generator again. Can you paste or provide a screenshot of what you see?

thomaskenne commented 3 years ago

hmm - is it 404-something?


  doording-cms php please ssg:generate
You may be able to speed up site generation significantly by installing spatie/fork and using multiple workers (requires PHP 8+).
[✔] Gathered content to be generated
Generating /til-opgangen
"/404"
Statamic\StaticSite\Page^ {#337
  #files: Illuminate\Filesystem\Filesystem^ {#112}
  #config: array:7 [
    "base_url" => "http://doording.test"
    "destination" => "/Users/thomas/Projects/doording-cms/storage/app/static"
    "copy" => array:4 [
      "/Users/thomas/Projects/doording-cms/public/css" => "css"
      "/Users/thomas/Projects/doording-cms/public/js" => "js"
      "/Users/thomas/Projects/doording-cms/public/img" => "img"
      "/Users/thomas/Projects/doording-cms/public/assets" => "assets"
    ]
    "symlinks" => []
    "urls" => []
    "exclude" => []
    "glide" => array:1 [
      "directory" => "img"
    ]
  ]
  #content: Statamic\StaticSite\Route^ {#471
    #url: "http://doording.test/404"
  }
[✔] Generated 0 content files
[✔] /Users/thomas/Projects/doording-cms/public/css copied to /Users/thomas/Projects/doording-cms/storage/app/static/css
[✔] /Users/thomas/Projects/doording-cms/public/js copied to /Users/thomas/Projects/doording-cms/storage/app/static/js
[✔] /Users/thomas/Projects/doording-cms/public/img copied to /Users/thomas/Projects/doording-cms/storage/app/static/img
[✔] /Users/thomas/Projects/doording-cms/public/assets copied to /Users/thomas/Projects/doording-cms/storage/app/static/assets
Static site generated into /Users/thomas/Projects/doording-cms/storage/app/static
´´´
jasonvarga commented 3 years ago

What's in your config/statamic/sites.php?

thomaskenne commented 3 years ago
return [
    'sites' => [

        'doording-dk' => [
            'name' => 'Doording (DK)',
            'locale' => 'da',
            'url' => 'http://doording-cms.test/',
        ],

        'uwt-en' => [
            'name' => 'UWT (EN)',
            'locale' => 'en',
            'url' => 'http://uwt.test/',
        ],

    ],
];
thomaskenne commented 3 years ago

Oh boy - I just found out now that my env('APP_URL') was wrong. Correcting that, fixed this issue.

@jasonvarga Thanks for taking the time and bringing the dd that helped me realize that.

jasonvarga commented 3 years ago

Thanks for following up!