Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support of Turbolinks 5 on the server side. #10

Merged
merged 15 commits into from
Jan 20, 2017

Conversation

tortuetorche
Copy link
Contributor

@tortuetorche tortuetorche commented Jul 29, 2016

Based on https://github.com/turbolinks/turbolinks-rails

FYI, jQuery Turbolinks doesn't support Turbolinks 5, see kossnocorp/jquery.turbolinks#56.
So you can use https://github.com/rstacruz/onmount or https://github.com/wshostak/turbolinks-jquery instead.

For Laravel framework support, see https://github.com/efficiently/turbolinks/tree/turbo5 https://github.com/frenzyapp/turbolinks/tree/3.1.0 and these snippets:

Put this code in your app/Http/Controllers/Controller.php file:

    //...

    /**
     * @param  string $path URL
     * @param  array $options Default to: ['status' => 302, 'headers' => [], 'secure' => null]
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function redirectTo($path, $options = [])
    {
        $defaultOptions = ['status' => 302, 'headers' => [], 'secure' => null];
        $options = array_merge($defaultOptions, $options);
        if (response()->hasMacro('redirectToWithTurbolinks')) {
            return response()->redirectToWithTurbolinks($path, $options);
        }
        return response()->redirectTo($path, $options['status'], $options['headers'], $options['secure']);
    }
    //...

NOTE: The redirectToWithTurbolinks() code is here.

Then in your app/Http/Controllers/MessagesController.php file:

    //...
    public function store()
    {
        $inputs = Input::except('_method', '_token');
        $validator = Validator::make($inputs, Message::$rules);

        if ($validator->passes() && $message = Message::create($inputs)) {
            return $this->redirectTo(route('messages.show', $message->id));
        }

        return $this->redirectTo(route('messages.create'))->withInput()
            ->with('error', "Error: Unable to save this message");
    }
    //...

Specific Turbolinks options of the $options parameter can be passed to the ->redirectTo() method (or ->redirectToWithTurbolinks() macro):

<?php
$options = ['turbolinks' => false]; // skip turbolinks redirection, if the `turbolinks` option is identical (===) to false
$options = []; // turbolinks default redirection behavior, the `replace` action
$options = ['turbolinks' => 0]; // turbolinks default redirection behavior, the `replace` action
$options = ['turbolinks' => '']; // turbolinks default redirection behavior, the `replace` action
$options = ['turbolinks' => null]; // turbolinks default redirection behavior, the `replace` action
$options = ['turbolinks' => 'any_other_string_than_advance']; // turbolinks default redirection behavior, the `replace` action
$options = ['turbolinks' => 'advance']; // turbolinks custom redirection, with the `advance` action

NOTE: During an advance redirection, Turbolinks pushes a new entry onto the browser’s history stack using history.pushState.


Related to #9
For Turbolinks 3, see #6

Have a good day,
Tortue Torche

@tortuetorche
Copy link
Contributor Author

tortuetorche commented Jul 29, 2016

By the way, You can download a Laravel demo application which uses Turbolinks 5, here:
https://github.com/efficiently/laravel_larasset_app/tree/turbo5-l5.1#readme

@carlalexander
Copy link
Contributor

Thanks! I'll try to look at it this week.

@tortuetorche
Copy link
Contributor Author

Hi @carlalexander,

Any news on this pull request?

Cheers,
Tortue Torche

@carlalexander
Copy link
Contributor

Sorry @tortuetorche, I ended up being swamped the last two weeks. There are conflicts with the merge that I need to look at. I also noticed some of the documentation badges are pointing to the wrong branch now.

I'll try to look at this over the weekend or next week. Sorry again for the delay.

@carlalexander
Copy link
Contributor

So I've looked over the PR, everything in the README.md points to the turbo5 branch and not master. I wouldn't remove Resources/public/js/turbolinks.js since other packages might have post install hooks for it. Instead, I'd replace it with the current turbolinks.js.

@tortuetorche
Copy link
Contributor Author

tortuetorche commented Aug 22, 2016

Hi @carlalexander,

Resources/public/js/turbolinks.js isn't removed, just updated with https://github.com/turbolinks/turbolinks/blob/v5.0.0/dist/turbolinks.js

For jQuery compatibility, there is a new library https://github.com/wshostak/turbolinks-jquery which is compatible with Turbolinks 5, but I didn't test it yet.
Maybe we can switch to it, but it'll be better to do this in a second pull request.

Have a good day

@tortuetorche
Copy link
Contributor Author

I updated this pull request description to explain how we can use Turbolinks options on the server side.

@carlalexander
Copy link
Contributor

Thanks @tortuetorche! Sorry, been swamped and haven't looked into the new stuff 😞

@UFOMelkor
Copy link

Works like a charm for me 👍

['turbolinks' => false] // skip turbolinks redirection, if the `turbolinks` option is identical (===) to false
[] // turbolinks default redirection behavior, the `replace` action
['turbolinks' => 0] // turbolinks default redirection behavior, the `replace` action
['turbolinks' => ''] // turbolinks default redirection behavior, the `replace` action
['turbolinks' => null] // turbolinks default redirection behavior, the `replace` action
['turbolinks' => 'any_other_string_than_advance'] // turbolinks default redirection behavior, the `replace` action
['turbolinks' => 'advance'] // turbolinks custom redirection, with the `advance` action
@tortuetorche tortuetorche changed the title Experimental support of Turbolinks 5 on the server side. Support of Turbolinks 5 on the server side. Jan 17, 2017
@tortuetorche
Copy link
Contributor Author

My pull request is ready to be merged 🚀
Of course, we can do some refactoring, add more tests and docs.
But, the main goal of porting Turbolinks 5 is done 👍

Happy New Year 2017 🎆

@carlalexander carlalexander merged commit 8033a28 into helthe:master Jan 20, 2017
@carlalexander
Copy link
Contributor

carlalexander commented Jan 20, 2017

Thanks for your all your work @tortuetorche! Version bumped to 2.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants