Routes.py to filter out file version numbers

49 views
Skip to first unread message

Ian W. Scott

unread,
Nov 17, 2017, 10:12:58 AM11/17/17
to web2py-users
Hi there. I'm dynamically adding version numbers to the filenames for static assets like css, js, and image files. (These version numbers aren't actually in the filenames. They're just added to the url in the <link> and <script> tags that reference them.) So I need to use url rewriting to remove those version numbers. In other words, if the browser looks for 

/static/css/theme.1510773357.css 

it should be served 

/static/css/theme.css

I've written a routes.py file that I thought would do the trick, but it's not working. The browser is still giving a 404 error for the file. 

routes_in = ((r'.*/static/(?P<subdir>(css|images|audio|js))/(?P<basename>.*)\.[\d]{10}\.(?P<extension>(css|js|ico|png|svg|jpe?g))',
              r
'/static/\g<subdir>/\g<basename>\.\g<extension>'),
             
)
routes_out
= [(x, y) for (y, x) in routes_in]

Any idea what's wrong? I've confirmed that the first regex in the pair does match the filename, and that the correct matching groups are found ({'subdir': 'css', 'basename': 'theme', 'extension': 'css'}). Is there something wrong with the second string that creates the new path?

In case anyone is wondering, I'm doing this even though I know about web2py's built-in versioning system. The app is under constant incremental development and I don't want to have to manually change version numbers every time I push out an update. This approach automatically forces users to download the updated file based on its last-modified datetime.

Thanks,

Ian

Ian W. Scott

unread,
Nov 17, 2017, 10:24:07 AM11/17/17
to web2py-users
I should add that one reason I'm having trouble debugging is because I'm not sure how to test the output of the rewrite. Is there any way to see exactly what the rule is going to output, given a particular filename?

Ian W. Scott

unread,
Nov 20, 2017, 12:04:27 PM11/20/17
to web2py-users
I'm still wondering if anyone can help with this. I found a minor error in the second member of the routes_in tuple (the period shouldn't be escaped). But it's still not working for me. I used re.sub to confirm that the second member should be outputting the right string. So far I'm just testing this on the bundled rocket server, so I don't think there's any issue with server settings.

Any help would be much appreciated.


On Friday, November 17, 2017 at 10:12:58 AM UTC-5, Ian W. Scott wrote:

Dave S

unread,
Nov 20, 2017, 8:38:17 PM11/20/17
to web2py-users


On Monday, November 20, 2017 at 9:04:27 AM UTC-8, Ian W. Scott wrote:
I'm still wondering if anyone can help with this. I found a minor error in the second member of the routes_in tuple (the period shouldn't be escaped). But it's still not working for me. I used re.sub to confirm that the second member should be outputting the right string. So far I'm just testing this on the bundled rocket server, so I don't think there's any issue with server settings.

Any help would be much appreciated.

I tried to run the doctests on rewrite.py, but didn't do it right to get the gluon imports working.  The module doesn't seem

Is the error that you're not getting the files you expect?  Are you getting a 404 as a result?

/dps

 

Ian W. Scott

unread,
Nov 20, 2017, 9:02:38 PM11/20/17
to web2py-users
Yes, that's right. I'm getting a 404 on each file with the added version string, which must mean that the routes.py isn't pointing the browser successfully to the real file (without the version string in the name.

For clarity's sake, here's the contents of my routes.py with the one syntax error fixed:

routes_in = ((r'.*/static/(?P<subdir>(css|images|audio|js))/(?P<basename>.*)\.[\d]{10}\.(?P<extension>(css|js|ico|png|svg|jpe?g))',
              r
'/static/\g<subdir>/\g<basename>.\g<extension>'),
             
)
routes_out
= [(x, y) for (y, x) in routes_in]

I fixed the syntax error, but I'm still getting 404 errors.

Dave S

unread,
Nov 20, 2017, 9:55:09 PM11/20/17
to web2py-users


On Monday, November 20, 2017 at 6:02:38 PM UTC-8, Ian W. Scott wrote:
Yes, that's right. I'm getting a 404 on each file with the added version string, which must mean that the routes.py isn't pointing the browser successfully to the real file (without the version string in the name.

For clarity's sake, here's the contents of my routes.py with the one syntax error fixed:

routes_in = ((r'.*/static/(?P<subdir>(css|images|audio|js))/(?P<basename>.*)\.[\d]{10}\.(?P<extension>(css|js|ico|png|svg|jpe?g))',
              r
'/static/\g<subdir>/\g<basename>.\g<extension>'),
             
)
routes_out
= [(x, y) for (y, x) in routes_in]

I fixed the syntax error, but I'm still getting 404 errors.


Does your 404 page show the URL?  The default web2py 404 will show either invalid controller or invalid function, but not invalid application.

/dps

Ian W. Scott

unread,
Nov 20, 2017, 10:04:00 PM11/20/17
to web2py-users
I'm not getting web2py's 404 page because it's not the controller's page that isn't found. It's the stylesheet. So when I view all of the file requests in Chrome's dev tools (network tab) I see that the request for the stylesheet has returned 404. The requested URL was:


Is that the problem? Does web2py's rewrite system only work for the controller functions, but not for static files?

Dave S

unread,
Nov 20, 2017, 11:02:38 PM11/20/17
to web2py-users
u

Dave S

unread,
Nov 20, 2017, 11:02:56 PM11/20/17
to web2py-users


On Monday, November 20, 2017 at 7:04:00 PM UTC-8, Ian W. Scott wrote:
I'm not getting web2py's 404 page because it's not the controller's page that isn't found. It's the stylesheet.

Yeah, I just was about to admit I didn't try messing with anything from static.
 
So when I view all of the file requests in Chrome's dev tools (network tab) I see that the request for the stylesheet has returned 404. The requested URL was:


Is that the problem? Does web2py's rewrite system only work for the controller functions, but not for static files?


Well, the default "static file versioning" works for static files, but I've forgotten where that is handled.

/dps
 

Ian W. Scott

unread,
Nov 20, 2017, 11:04:36 PM11/20/17
to web2py-users
I saw it the other day, so I can find it again. I just didn't want to be bothered manually updating version numbers all the time. So I was looking to build something more automatic. Oh well. Thanks for your help, anyway.

Ian
Reply all
Reply to author
Forward
0 new messages