timber icon indicating copy to clipboard operation
timber copied to clipboard

Timber custom Route::map throws 404

Open moazam1 opened this issue 5 years ago • 8 comments

We have created custom routes using Timber routing. They work as expected, we see output fine in the browser but recently we found that Googlebot was complaining we are showing 404 status on existings pages. This issue is only happening with custom routes. I created following test route:

Routes::map('this-is-a-test/', function ($params) {
    http_response_code(200);
    echo 'Done'; // I also tried Routes::load('test.php', $params); both shows same result
    exit;
});

And then I opened http://mysite.test/this-is-a-test/ url in the Chrome it was showing 200 which was perfect but when I tried using curl command it showed me 404.

λ curl -I http://mysite.test/this-is-a-test/

HTTP/1.1 404 Not Found
Date: Fri, 23 Apr 2021 03:44:00 GMT
Server: Apache/2.4.39 (Win64) OpenSSL/1.1.1b PHP/7.3.8
Strict-Transport-Security: max-age=31536000
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Link: <http://mysite.test/wp-json/>; rel="https://api.w.org/"
Content-Type: text/html; charset=UTF-8

This is definitely not a desirable behavior. The only solution worked for me is when I added http_response_code(200); code in the wp action it showed me 200. I do not want to do that because it's going to mess with other pages.

How can I fix this issue? Any help will be greatly appreciated. Thanks

moazam1 avatar Apr 23 '21 03:04 moazam1

It just doesn't work… see here.

if ( $wp_version >= 4.7 ) {
	$this->markTestSkipped('Something broke in WP 4.7, but this is deprecated anyway');
} else {
	$this->assertTrue($template);
}

mileyvirus avatar May 19 '21 15:05 mileyvirus

I'm having the same issue...

itsHall avatar May 25 '21 19:05 itsHall

@moazam1 have you found a solution for this problem yet?

itsHall avatar May 27 '21 20:05 itsHall

@jarednova is there any workaround you can think of to this issue?

itsHall avatar Jun 04 '21 17:06 itsHall

It just doesn't work… see here.

if ( $wp_version >= 4.7 ) {
	$this->markTestSkipped('Something broke in WP 4.7, but this is deprecated anyway');
} else {
	$this->assertTrue($template);
}

@itsHall they officially deprecated the routing function if you have WordPress version 4.7+. They completely removed it in the upcoming Timber 2.0.

mileyvirus avatar Jun 09 '21 19:06 mileyvirus

@mileyvirus sorry for the very late reply but is there an alternative method to setting up custom routing now?

@moazam1 did you remove your custom routes or were you able to find a solution?

itsHall avatar Jul 13 '21 16:07 itsHall

@itsHall Routing will still be available if you add it yourself. Timber will not ship with a default routing library. Many people don’t use routing and some people prefer to use a different routing library than the default Upstatement/routes package that is currently shipped with Timber.

If you still want to use the routing library with Timber 2.0, you can install it with Composer:

composer require upstatement/routes

Or you can use any other library listed here: https://timber.github.io/docs/v2/guides/routing/. I can probably improve the information listed there.

Unfortunately, I can’t help with the originally reported issue. Maybe it would help to also report it as an issue in https://github.com/Upstatement/routes/issues.

gchtr avatar Jul 15 '21 17:07 gchtr

@gchtr thank you for the info! I ended up recreating my routes using WP's routing method. Thank you for working on Timber.

itsHall avatar Jul 27 '21 15:07 itsHall