Created
December 12, 2015 02:51
-
-
Save ivanyv/ccf9a08b2423bbdef6d7 to your computer and use it in GitHub Desktop.
Easy Google AMP on Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<link rel="canonical" href="<%= current_uri_sans_amp %>"> | |
</head> | |
... | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<link rel="amphtml" href="/amp<%= current_uri %>"> | |
<link rel="canonical" href="<%= current_uri %>"> | |
</head> | |
... | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/application_controller.rb | |
class ApplicationController < ActionController::Base | |
# Prevent CSRF attacks by raising an exception. | |
# For APIs, you may want to use :null_session instead. | |
protect_from_forgery with: :exception | |
before_action :force_amp, if: -> { request.path_parameters[:amp] } | |
private | |
def force_amp | |
request.format = 'amp' | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/mime_types.rb | |
Mime::Type.register 'text/html', :amp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/routes.rb | |
Rails.application.routes.draw do | |
scope '(:amp)', constraints: { amp: /amp/ } do | |
# Put here all routes that also have AMP variants | |
root to: 'home#index' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment