Class: Nanoc2::Routers::Versioned

Inherits:
Nanoc2::Router show all
Defined in:
lib/nanoc2/routers/versioned.rb

Overview

The versioned router behaves pretty much like the default router, with the exception that asset representations (but not page representations) can have versions which will be added to generated URLs.

This is very useful if you want to cache assets aggressively by sending an Expires header to clients. An Expires header usually has the issue that clients will not request the asset even if it has changed; giving the asset a different version will cause the URL to be changed, which in turn will cause the client to request the new asset.

Example

For example, the URL of a textual asset containing the CSS stylesheet with its ‘version’ attribute set to 28 will become /assets/style-v28.css.

To link to the stylesheet in a DRY way, give the asset a unique name (such as ‘style’) and then do something along this way:

<link rel="stylesheet"
      type="text/css"
      media="screen"
      href="<%= @assets.find { |a| a.asset_id == 'style' }.path %>">

Constant Summary

Constants inherited from Plugin

Plugin::MAP

Instance Method Summary collapse

Methods inherited from Nanoc2::Router

#disk_path_for, #initialize, #web_path_for

Methods inherited from Plugin

identifier, identifiers, named, register

Constructor Details

This class inherits a constructor from Nanoc2::Router

Instance Method Details

#path_for_asset_rep(asset_rep) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/nanoc2/routers/versioned.rb', line 50

def path_for_asset_rep(asset_rep)
  # Get data we need
  extension     = asset_rep.attribute_named(:extension)
  modified_path = asset_rep.asset.path[0..-2]
  version       = asset_rep.attribute_named(:version)

  # Initialize path
  assets_prefix = @site.config[:assets_prefix] || '/assets'
  path = assets_prefix + modified_path

  # Add version if necessary
  unless version.nil?
    path += '-v' + version.to_s
  end

  # Add rep name
  unless asset_rep.name == :default
    path += '-' + asset_rep.name.to_s
  end

  # Add extension
  path += '.' + extension

  # Done
  path
end

#path_for_page_rep(page_rep) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nanoc2/routers/versioned.rb', line 30

def path_for_page_rep(page_rep)
  # Get data we need
  filename   = page_rep.attribute_named(:filename)
  extension  = page_rep.attribute_named(:extension)

  # Initialize path
  path = page_rep.page.path + filename

  # Add rep name if necessary
  unless page_rep.name == :default
    path += '-' + page_rep.name.to_s
  end

  # Add extension
  path += '.' + extension

  # Done
  path
end