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

Include RAILS_RELATIVE_URL_ROOT environment variable in publicPath. #1428

Merged
merged 7 commits into from
Apr 17, 2018

Conversation

jpickwell
Copy link
Contributor

Fix #1357.


// Add prefix to publicPath.
if (process.env.RAILS_RELATIVE_URL_ROOT) {
config.publicPath = `/${process.env.RAILS_RELATIVE_URL_ROOT}${config.publicPath}`
Copy link
Member

@gauravtiwari gauravtiwari Apr 16, 2018

Choose a reason for hiding this comment

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

Could you add some test for this, please? using different paths

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about that, but wasn't sure which test file to put them in? I was thinking of using the config test file, but wasn't sure because I didn't see anything in there for publicPath.

Copy link
Member

Choose a reason for hiding this comment

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

Ahh yes, but could you please add one :)

/* global test expect, describe */

const { chdirTestApp, chdirCwd } = require('../utils/helpers')

chdirTestApp()

describe('Config', () => {
  beforeEach(() => jest.resetModules())
  afterAll(chdirCwd)

  test('public path', () => {
    delete process.env.RAILS_RELATIVE_URL_ROOT
    const config = require('../config')
    expect(config.publicPath).toEqual('/packs/')
  })

  test('public path with relative root', () => {
    process.env.RAILS_RELATIVE_URL_ROOT = '/foo'
    const config = require('../config')
    expect(config.publicPath).toEqual('/foo/packs/')
  })

  test('should return extensions as listed in app config', () => {
    const config = require('../config')
    expect(config.extensions).toEqual([
      '.js',
      '.sass',
      '.scss',
      '.css',
      '.module.sass',
      '.module.scss',
      '.module.css',
      '.png',
      '.svg',
      '.gif',
      '.jpeg',
      '.jpg'
    ])
  })
})

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do. Thanks for the boilerplate.

Copy link
Member

Choose a reason for hiding this comment

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

Great thanks 👍

Copy link
Member

@gauravtiwari gauravtiwari Apr 17, 2018

Choose a reason for hiding this comment

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

I guess we need to set RAILS_ENV in the test for consistent results:

 test('public path', () => {
    process.env.RAILS_ENV = 'development'
    delete process.env.RAILS_RELATIVE_URL_ROOT
    const config = require('../config')
    expect(config.publicPath).toEqual('/packs/')
  })

  test('public path with relative root', () => {
    process.env.RAILS_ENV = 'development'
    process.env.RAILS_RELATIVE_URL_ROOT = '/foo'
    const config = require('../config')
    expect(config.publicPath).toEqual('/foo/packs/')
  })

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I assume you meant NODE_ENV in your examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, you meant RAILS_ENV.

Copy link
Member

Choose a reason for hiding this comment

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

oh yes! I guess you figured it out 😉

@rails rails deleted a comment from inkhrt Apr 17, 2018
}

// Remove extra slashes.
config.publicPath = config.publicPath.replace(/(^\/|[^:]\/)\/+/g, '$1')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The [^:]\/ part doesn't seem correct because if public_output_path contains a protocol, then publicPath would have a leading slash; e.g., (ignoring relative root) public_output_path='https://cdn.com' and publicPath='/https://cdn.com/'. The protocol would be preserved with the current RegExp, but that doesn't appear to be correct. Maybe I'm missing something.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, but at the moment Rails handles the CDN side of things so it's fine but you are right this needs more check if we expect the public_output_path to have a protocol as well (which we don't at the moment).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good.

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.

2 participants