0

Say I have the following structure for 3 different apps that live on 3 different domains, and a personal "cdn" where I can put files that are common across all three apps:

root/
     /dinosaureggs.com
     /magicalapples.com
     /wizardsupplies.com
     /mypersonalcdn.com
          /js
          /php
          /css

This same structure is mirrored on a local and production environment.

If I'm in dinosaureggs.com and I want to use a shared PHP library in the /php folder I can say:

require('/../mypersonalcdn.com/php/library.php')

This works fine on both the local and production servers.

However, it won't work for JS because JS includes can't back out of the "app" with paths the same way PHP can.

I can't say:

<script type="text/javascript" src="/../mypersonal.cdn/js/library.js"></script>

Now, on live I could say:

<script type="text/javascript" src="http://mypersonalcdn.com/js/menus.js"></script>

But this doesn't solve my problem for local environment (at least to the extent that I wish to leave it truly locally and have the option to develop offline).

Any suggestions?

sbuck
  • 1,846
  • 4
  • 24
  • 40

4 Answers4

0

While the PHP to fetch JS method above was ok, this was a much more straightforward solution that I eventually landed on:

Serving files outside of doc root / having common client files across multiple domains

To do this you set a mod_alias in http.conf

On local environment that looked like this:

Alias /shared/ "/Users/Jane Doe/Sites/shared"
<Directory "/Users/Jane Doe/Sites/shared">
    Options Indexes MultiViews
    Order allow,deny
    Allow from all
</Directory>

This makes it so you can call a css (or image or js or whatever) like this:

But instead of looking in /shared/ on that domain, it'd go to wherever on the server you had "shared" pointed to; in this case: /Users/Jane Doe/Sites/shared/

Resources:

Community
  • 1
  • 1
sbuck
  • 1,846
  • 4
  • 24
  • 40
0

Each app has to provide it's own copy of the JS library, either that or put them in your CDN.

You can't go up from your webroot because there is nothing there, so make sure it's available within your webroot, either because it's there or by a rewrite that forwards the request to a PHP script that will find the JS file and return it's contents.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
  • Ended up going the route of your last suggestion. Used a minify method that will fetch the js via php and compress it down to a js file that gets generated in the appropriate app/domain. Used: http://www.electrictoolbox.com/combine-minify-javascript-php/ – sbuck Dec 09 '11 at 05:47
0

For local devopment, why can't you just use the html file scheme for src paramaters.

For example, (I'm making up the directory where your root folder is located here, change it)

<script type="text/javascript" src="file://Users/bob/projects/root/mypersonalcdn.com/js/menus.js"></script>
MEURSAULT
  • 8,307
  • 4
  • 24
  • 23
  • It's local development, but it's being run on a local server (via MAMP), so file:// type stuff won't work. – sbuck Dec 09 '11 at 03:49
0

You can modify your php file like this:

<?php if ($_SERVER['SERVER_NAME'] == "localhost") { ?>
    <script type="text/javascript" src="path/to/your/library.js"></script>
<?php } else { ?>
    <script type="text/javascript" src="http://mypersonalcdn.com/js/menus.js"></script>
<?php } ?>

It's a common way to deal with such problems.

Shawn
  • 311
  • 3
  • 5
  • using path/to/your/library.js will require me having it within the folders of that specific / app domain, which means each app / domain has to have a copy. This is what I'm trying to get around. – sbuck Dec 09 '11 at 03:50