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

Deprecate asset fallback #375

Merged
merged 8 commits into from
Sep 1, 2016
Merged

Conversation

schneems
Copy link
Member

When an asset isn't found the behavior is to pass the string through. For example a valid asset will return a url from pipeline

asset_path("application.js")
# => assets/application-123098udasvi0mnafd.js

While if you make a typo, you won't get an error or anything, it just falls through:

asset_path("app1icati0n.js")
# => "app1icati0n.js"

Hopefully I don't have to elaborate on why this is bad.

This PR is a child PR to one in Rails that will introduce a public_asset_path API.

There are valid reasons for not using the asset pipeline, if you have a purely static asset in the public folder or if you want to link to a static URL in your assets somewhere it makes sense to declare that intention. Eventually we will replace the behavior of the deprecation with an exception so people don't lose hours of their to typos.

We only emit the deprecation when a public_ api is available in Rails. This means that sprockets-rails can still be used in a backwards compatible manner.

When an asset isn't found the behavior is to pass the string through. For example a valid asset will return a url from pipeline

```
asset_path("application.js")
# => assets/application-123098udasvi0mnafd.js
```

While if you make a typo, you won't get an error or anything, it just falls through:

```
asset_path("app1icati0n.js")
# => "app1icati0n.js"
```

Hopefully I don't have to elaborate on why this is bad.

This PR is a child PR to one in Rails that will introduce a `public_asset_path` API.

There are valid reasons for not using the asset pipeline, if you have a purely static asset in the public folder or if you want to link to a static URL in your assets somewhere it makes sense to declare that intention. Eventually we will replace the behavior of the deprecation with an exception so people don't lose hours of their to typos.

We only emit the deprecation when a `public_` api is available in Rails. This means that sprockets-rails can still be used in a backwards compatible manner.
@schneems
Copy link
Member Author

The Rails PR is rails/rails#26226

Instead of relying on hard coded indexes, we can look through the called method stack backwards (i.e. the first method called will be the first in the list). If it has a suffix for one of our APIs, then we check to see if there exists a `public_` prefix on the view, if it does it's the first method called that can be deprecated so we point to that.
Use `public_folder: true` instead.
- Use `public_folder: true` instead.
- Allow configurable deprecation/error behavior via **`config.assets.unknown_asset_fallback`**

When set to a truthy value, the a result will be returned even if the requested asset is not found in the asset pipeline. When set to `false` it will raise an error.
@@ -6,6 +6,7 @@
module Sprockets
module Rails
module Helper
class AssetNotFound < StandardError; end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we add a newline after this class definition?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

message << "The public fallback behavior is being deprecaed and will be removed.\n"
message << "pass in `skip_pipeline: true` instead.\n"

call_stack = respond_to?(:caller_locations) ? caller_locations : caller
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're on a Rails new enough to have public_compute_asset_path, that implies we're on a Ruby new enough to have caller_locations, doesn't it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests don't actually use Rails 5.1, we define the method, and the tests get run on all Ruby versions. The alternative to this would be detecting ruby versions and skipping the tests, which is less clean. I also want to be able to make a gem so that older versions of Rails can use this flag to get the error/deprecation behavior.

@kaspth
Copy link

kaspth commented Aug 31, 2016

Does @matthewd's comment about caller_locations stand? Otherwise :shipit: 👏

@schneems schneems merged commit e3dc15c into master Sep 1, 2016
@rafaelfranca rafaelfranca deleted the schneems/deprecate-fallback-behavior branch August 31, 2017 20:32
@rlue
Copy link

rlue commented Jan 6, 2018

I know it's been a long time since this PR was merged. Is there any chance we could get an update to the section of the Rails Guides that addresses asset fallback?

I'd be glad to submit a PR with the approval of the team (but otherwise, I'm reluctant to spend any time on it).

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.

None yet

4 participants